ObjectListView drag and drop to RichTextBox

本文关键字:to RichTextBox drop and drag ObjectListView | 更新日期: 2023-09-27 18:20:38

所以我有一个对象列表视图(实际上是一个树列表视图)。我希望能够将一个项目从中拖到richtextbox中,并让它插入被拖项目的属性(在本例中为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;
    }

我哪里错了?

干杯

ObjectListView drag and drop to RichTextBox

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