开发者

Drag drop between two listboxes with Values

I have 2 list boxes, one is DataBound, which has DisplayMeber and a ValueMember. I need to drag and drop entries from DataBound ListBox to other.

I tried, can get Display text. But not the value. How can get the Display Text + Value to destination.

 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 = lbFavS开发者_如何学JAVAupp.FindString(selectedManufacturer);
                    if (found < 0)
                    {
                        lbFav.Items.Add(selectedManufacturer);
                    }
                }
            }
        }
    }


You can cast the sender as a listbox and read its selected value. If you are handling dragging from onelistbox in a MouseDown event, then something like the following should work.

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

In this case my listbox datasource is collection of System.Collections.DictionaryEntry.

So in the dragdrop event I can read the selected value like the following.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜