【问题标题】:Drag drop between two listboxes with Values在两个带有值的列表框之间拖放
【发布时间】:2011-06-07 08:56:43
【问题描述】:

我有 2 个列表框,一个是 DataBound,它有 DisplayMeber 和一个 ValueMember。我需要将条目从 DataBound ListBox 拖放到其他条目。

我试过了,可以得到显示文本。但不是价值。如何将 Display Text + Value 获取到目的地。

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

【问题讨论】:

    标签: c# .net drag-and-drop listbox


    【解决方案1】:

    您可以将发件人转换为列表框并读取其选定值。 如果您在 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 的集合。

    所以在拖放事件中,我可以像下面这样读取选定的值。

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

    【讨论】:

    • 正确,但如何添加到 Destination.. lbFav.Items.Add() 没有办法添加...
    • 当你拥有它时,你可以做任何事情
    • @Buddhi with destinaiton 你的意思是添加一个项目到一个特定的地方吗?比如将 itemX 添加到列表框的第 3 行?
    • 您可以在拖放事件中创建新项目并将其添加到目标项目,所以是的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    • 2020-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多