ObjectListView拖放到RichTextBox

时间:2012-01-16 14:52:11

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

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

TreeListView的objectmodel是名为List<T>的类的SpecItem

这是我到目前为止所做的:

    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;
    }

我哪里错了?

干杯

1 个答案:

答案 0 :(得分: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