• <bdo id='CSpxp'></bdo><ul id='CSpxp'></ul>
  1. <i id='CSpxp'><tr id='CSpxp'><dt id='CSpxp'><q id='CSpxp'><span id='CSpxp'><b id='CSpxp'><form id='CSpxp'><ins id='CSpxp'></ins><ul id='CSpxp'></ul><sub id='CSpxp'></sub></form><legend id='CSpxp'></legend><bdo id='CSpxp'><pre id='CSpxp'><center id='CSpxp'></center></pre></bdo></b><th id='CSpxp'></th></span></q></dt></tr></i><div id='CSpxp'><tfoot id='CSpxp'></tfoot><dl id='CSpxp'><fieldset id='CSpxp'></fieldset></dl></div>
  2. <legend id='CSpxp'><style id='CSpxp'><dir id='CSpxp'><q id='CSpxp'></q></dir></style></legend>

    <tfoot id='CSpxp'></tfoot>

    <small id='CSpxp'></small><noframes id='CSpxp'>

    1. ListBox MultiSelect 拖放问题

      时间:2023-08-25
          <bdo id='Q1JaO'></bdo><ul id='Q1JaO'></ul>
            <tbody id='Q1JaO'></tbody>

          <small id='Q1JaO'></small><noframes id='Q1JaO'>

        • <i id='Q1JaO'><tr id='Q1JaO'><dt id='Q1JaO'><q id='Q1JaO'><span id='Q1JaO'><b id='Q1JaO'><form id='Q1JaO'><ins id='Q1JaO'></ins><ul id='Q1JaO'></ul><sub id='Q1JaO'></sub></form><legend id='Q1JaO'></legend><bdo id='Q1JaO'><pre id='Q1JaO'><center id='Q1JaO'></center></pre></bdo></b><th id='Q1JaO'></th></span></q></dt></tr></i><div id='Q1JaO'><tfoot id='Q1JaO'></tfoot><dl id='Q1JaO'><fieldset id='Q1JaO'></fieldset></dl></div>
          <tfoot id='Q1JaO'></tfoot>

            • <legend id='Q1JaO'><style id='Q1JaO'><dir id='Q1JaO'><q id='Q1JaO'></q></dir></style></legend>

              1. 本文介绍了ListBox MultiSelect 拖放问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试在 Windows 窗体中的 ListBox 之间拖放多个项目.我遇到的问题是,如果我选择多个按住 Shift 键的项目并尝试在不释放键的情况下拖放它,我会收到错误消息.实际上 SelectedIndices 和 SelectedItems 只显示 1 个项目,即我首先单击的那个,即使多个项目在 ListBox 中突出显示.

                I'm trying to do a drag and drop of multiple items between ListBox in windows forms. The problem I'm having is if I select multiple items holding the Shift key and try to drag and drop it without release the the key I get an error. Actually the SelectedIndices and SelectedItems just show 1 item, the one I clicked first, even though multiple items are highlighted in the ListBox.

                我正在使用 SelectionMode = MultiExtended

                I'm using SelectionMode = MultiExtended

                void ZListBox_MouseMove(object sender, MouseEventArgs e)
                {
                    if (isDraggingPoint.HasValue && e.Button == MouseButtons.Left && SelectedIndex >= 0)
                    {
                        var pointToClient = PointToClient(MousePosition);
                
                        if (isDraggingPoint.Value.Y != pointToClient.Y)
                        {
                            lastIndexItemOver = -1;
                            isDraggingPoint = null;
                
                            var dropResult = DoDragDrop(SelectedItems, DragDropEffects.Copy);
                        }
                    }
                }
                

                似乎如果我在执行DoDragDrop"之前不释放鼠标左键,则不会选择项目,并且如果我尝试从另一个 ListBox 获取 SelectedIndices,则计数是选定的项目",但是当我尝试浏览列表时,我得到一个 IndexOutOfRangeException.

                It seems that if I don't release the left mouse button before I do "DoDragDrop", the items aren't selected and also if I try to get the SelectedIndices from the other ListBox, the Count is the number of "selected items", but when I try to navigate the list, I get a IndexOutOfRangeException.

                有什么解决办法吗?

                重现问题的示例代码:(重现:1- 选择一个项目2- 按住 shift 并单击另一个项目,而不是释放 shift 和鼠标按钮,拖动此项目(如果您在if"中有断点,您将在 SelectedItems 上看到只有 1 个项目))

                Sample code to reproduce the issue: (To reproduce: 1- Select an item 2- Hold shift and click in another item, than without release shift and mouse button, drag this item (if you have a breakpoint inside the 'if', you'll see just 1 item on SelectedItems))

                public partial class Form1 : Form
                    {
                        public Form1()
                        {
                            InitializeComponent();
                            Load += Form1_Load;
                        }
                
                        private void Form1_Load(object sender, EventArgs e)
                        {
                            var someList = new List<ListItemsTest>();
                            someList.Add(new ListItemsTest() { ID = 1, Name = "Name 1" });
                            someList.Add(new ListItemsTest() { ID = 2, Name = "Name 2" });
                            someList.Add(new ListItemsTest() { ID = 3, Name = "Name 3" });
                            someList.Add(new ListItemsTest() { ID = 4, Name = "Name 4" });
                            someList.Add(new ListItemsTest() { ID = 5, Name = "Name 5" });
                            listBox1.DisplayMember = "Name";
                            listBox1.ValueMember = "ID";
                            listBox1.DataSource = someList;
                            listBox1.SelectionMode = SelectionMode.MultiExtended;
                            listBox1.MouseMove += ListBox1_MouseMove;
                            listBox1.AllowDrop = true;
                        }
                
                        void ListBox1_MouseMove(object sender, MouseEventArgs e)
                        {
                            if (e.Button == MouseButtons.Left && listBox1.SelectedIndex >= 0)
                            {
                                var dropResult = DoDragDrop(listBox1.SelectedItems, DragDropEffects.Copy);
                            }
                        }
                
                        public class ListItemsTest
                        {
                            public int ID { get; set; }
                            public string Name { get; set; }
                        }
                    }
                

                推荐答案

                只是为了让你知道我找到了另一个解决方案.如果我在 MouseDown 事件中设置 Capture = false,项目将按预期工作,我们不需要手动选择.

                Just to let you know I found another solution. If I set Capture = false in the MouseDown event, Items will work as expected and we don't need to do the manual selection.

                例如:

                void ZListBox_MouseDown(object sender, MouseEventArgs e)
                {
                    if (e.Button == MouseButtons.Left)
                    {
                        Capture = false;
                    }
                }
                

                希望对你有帮助!

                这篇关于ListBox MultiSelect 拖放问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:WPF 拖放 - 从 DragEventArgs 获取原始源信息 下一篇:c# 从我的自定义应用拖放到记事本

                相关文章

                最新文章

              2. <small id='ziR5v'></small><noframes id='ziR5v'>

                • <bdo id='ziR5v'></bdo><ul id='ziR5v'></ul>

                <i id='ziR5v'><tr id='ziR5v'><dt id='ziR5v'><q id='ziR5v'><span id='ziR5v'><b id='ziR5v'><form id='ziR5v'><ins id='ziR5v'></ins><ul id='ziR5v'></ul><sub id='ziR5v'></sub></form><legend id='ziR5v'></legend><bdo id='ziR5v'><pre id='ziR5v'><center id='ziR5v'></center></pre></bdo></b><th id='ziR5v'></th></span></q></dt></tr></i><div id='ziR5v'><tfoot id='ziR5v'></tfoot><dl id='ziR5v'><fieldset id='ziR5v'></fieldset></dl></div>

                  <tfoot id='ziR5v'></tfoot>
                    <legend id='ziR5v'><style id='ziR5v'><dir id='ziR5v'><q id='ziR5v'></q></dir></style></legend>