2011-06-07 35 views
0

我有兩個列表框,一個是DataBound,它具有DisplayMeber和一個ValueMember。我需要從DataBound ListBox拖放條目到其他條目。使用值在兩個列表框之間拖放

我試過了,可以得到顯示文字。但不是價值。如何獲得顯示文本+值到目的地。

private void lbFav_DragDrop(object sender, DragEventArgs e) 
    { 
     if (e.Data.GetDataPresent(DataFormats.StringFormat)) 
     { 
      string selectedManufacturer = e.Data.GetData(DataFormats.StringFormat) as string; 
      if (!string.IsNullOrEmpty(selectedManufacturer)) 
      { 
       if (selectedManufacturer.StartsWith("M_")) 
       { 
        selectedManufacturer = selectedManufacturer.Substring(2, (selectedManufacturer.Length - 2)); 
        int found = lbFavSupp.FindString(selectedManufacturer); 
        if (found < 0) 
        { 
         lbFav.Items.Add(selectedManufacturer); 
        } 
       } 
      } 
     } 
    } 

回答

2

您可以將發件人轉換爲列表框並讀取其選定值。 如果您正在處理MouseDown事件中的onelistbox拖動,則應如下所示。

 int index = listBox1.IndexFromPoint(e.X, e.Y); 
     var s = listBox1.Items[index]; //Putting item instead of 
     DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.All); 
     if (dde1 == DragDropEffects.All) 
     { 
      listBox1.Items.RemoveAt(listBox1.IndexFromPoint(e.X, e.Y)); 
     } 
    } 

在這種情況下我的列表框的數據源是System.Collections.DictionaryEntry的集合。

所以在dragdrop事件中,我可以像下面這樣讀取選定的值。

if (e.Data.GetDataPresent("System.Collections.DictionaryEntry")) 
{ 
    System.Collections.DictionaryEntry r = (System.Collections.DictionaryEntry)e.Data.GetData("System.Collections.DictionaryEntry"); 

    //Use r.Key or r.Value. 
    lbFav.Items.Add(r.Key);! 
} 
+0

正確的,但如何增加目的地.. lbFav.Items.Add()不具有增加的一種方式...... – 2011-06-07 09:12:28

+0

當你擁有它,就可以做任何事情 – DeveloperX 2011-06-07 09:14:13

+0

@Buddhi與destinaiton你的意思是添加一個項目到一個特定的地方嗎?就像添加itemX到列表框的第3行? – Bastardo 2011-06-07 10:31:40

相關問題