2012-01-16 61 views
1

所以我有一個objectlistview(實際上是一個treelistview)。我希望能夠從這個拖動一個項目到一個RichTextBox,並將它插入拖項的屬性(在這種情況下Default_Heirarchy_IDObjectListView拖放到RichTextBox

的TreeListView的objectmodel一個名爲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; 
    } 

我要去哪裏錯了?

乾杯

回答

1

拖動是您想要從(您的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