【问题标题】:ObjectListView drag and drop to RichTextBoxObjectListView 拖放到 RichTextBox
【发布时间】:2012-01-16 14:52:11
【问题描述】:

所以我有一个objectlistview(实际上是一个treelistview)。我希望能够将一个项目从这里拖到一个富文本框中,并让它插入被拖项目的属性(在本例中为Default_Heirarchy_ID

TreeListView 的对象模型是一个名为SpecItem 的类的List<T>

这是我目前所拥有的:

    public frmAutospecEditor(SpecItem siThis_, List<SpecItem> lstStock_)
    {
        InitializeComponent();

        txtFormula.DragEnter += new DragEventHandler(txtFormula_DragEnter);
        txtFormula.DragDrop += new DragEventHandler(txtFormula_DragDrop);
        ...
    }

    void txtFormula_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
    }

    private void tlvSpecItem_ItemDrag(object sender, ItemDragEventArgs e)
    {
        int intID = ((SpecItem)tlvSpecItem.GetItem(tlvSpecItem.SelectedIndex).RowObject).Default_Heirarchy_ID ??0;
        DoDragDrop(intID, DragDropEffects.Copy);
    }
    private void txtFormula_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
    {

        object objID = e.Data.GetData(typeof(String)); 
        //this is where it goes wrong - no matter what I try to do with this, it 
        //always returns either null, or the text displayed for that item in the TreeListView,               
        //NOT the ID as I want it to.
        string strID = (string)objID;
        txtFormula.Text = strID;
    }

我哪里出错了?

干杯

【问题讨论】:

    标签: c# drag-and-drop richtextbox objectlistview


    【解决方案1】:

    Drag 是您要从(您的 OLV)获取数据的控件。 Drop 是目标控件(您的文本框)。所以:

    将 OLV 的 IsSimpleDragSource 属性设置为 true。

    在文本框中将AllowDrop 属性设置为true。然后处理文本框的DragEnter 事件并使用DragEventArgs 参数。

    处理 ModelDropped 事件:

    private void yourOlv_ModelDropped(object sender, ModelDropEventArgs e) 
    { 
       // If they didn't drop on anything, then don't do anything 
       if (e.TargetModel == null) return; 
    
       // Use the dropped data: 
       // ((SpecItem)e.TargetModel) 
       // foreach (SpecItem si in e.SourceModels) ...
    
       // e.RefreshObjects(); 
    }
    

    阅读更多:http://objectlistview.sourceforge.net/cs/dragdrop.html#ixzz1lEt7LoGr

    【讨论】:

      猜你喜欢
      • 2015-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-02
      • 1970-01-01
      相关资源
      最近更新 更多