C# ObjectListView拖放到RichTextBox

C# ObjectListView拖放到RichTextBox,c#,drag-and-drop,richtextbox,objectlistview,C#,Drag And Drop,Richtextbox,Objectlistview,所以我有一个objectlistview(实际上是一个treelistview)。我希望能够将一个项目从这里拖到richtextbox上,并让它插入被拖项目的属性(在本例中为Default\u Heirarchy\u ID) TreeListView的objectmodel是一个名为SpecItem的类的列表 这就是我到目前为止所做的: public frmAutospecEditor(SpecItem siThis_, List<SpecItem> lstStock_)

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

TreeListView的objectmodel是一个名为
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;
    }
public frmAutospecEditor(SpecItem siThis,List lstStock)
{
初始化组件();
txtFormula.DragEnter+=新的DragEventHandler(txtFormula\u DragEnter);
txtFormula.DragDrop+=新的DragEventHandler(txtFormula\u DragDrop);
...
}
void txtFormula_DragEnter(对象发送方,DragEventArgs e)
{
e、 效果=DragDropEffects.Copy;
}
私有void tlvSpecItem_ItemDrag(对象发送方,ItemDragEventArgs e)
{
int intID=((SpecItem)tlvSpecItem.GetItem(tlvSpecItem.SelectedIndex.RowObject)。默认继承人ID??0;
DoDragDrop(intID,DragDropEffects.Copy);
}
私有void txtFormula_DragDrop(对象发送方,System.Windows.Forms.DragEventArgs e)
{
objectobjid=e.Data.GetData(typeofString));
//这就是它出错的地方——不管我用它做什么,它都会出错
//始终返回null或在TreeListView中为该项显示的文本,
//不是我想要的身份证。
string strID=(string)objID;
txtFormula.Text=strID;
}
我哪里做错了


干杯

拖动是要从(OLV)获取数据的控件。 下拉菜单是目标控件(您的文本框)。因此:

将OLV的
IsSimpleDragSource
属性设置为true

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

处理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(); 
}

阅读更多信息:

拖动是要从(OLV)获取数据的控件。 下拉菜单是目标控件(您的文本框)。因此:

将OLV的
IsSimpleDragSource
属性设置为true

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

处理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(); 
}
阅读更多: