1. <tfoot id='GIlYx'></tfoot>
    2. <small id='GIlYx'></small><noframes id='GIlYx'>

    3. <legend id='GIlYx'><style id='GIlYx'><dir id='GIlYx'><q id='GIlYx'></q></dir></style></legend>

        <bdo id='GIlYx'></bdo><ul id='GIlYx'></ul>

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

        C# 拖动 &amp;从列表框拖放到树视图

        时间:2023-08-25

              <tbody id='HNDFm'></tbody>

              <bdo id='HNDFm'></bdo><ul id='HNDFm'></ul>

              • <tfoot id='HNDFm'></tfoot>

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

                  <legend id='HNDFm'><style id='HNDFm'><dir id='HNDFm'><q id='HNDFm'></q></dir></style></legend>
                  本文介绍了C# 拖动 &amp;从列表框拖放到树视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个带有列表框和树视图的 winform.

                  I have a winform with a listbox and a treeview.

                  一旦我的列表框充满了项目,我想将它们(多个或单个)从列表框中拖放到树视图中的一个节点中.

                  Once my listbox is filled with items, I want to drag them (multiple or single) from the listbox and drop them in a node in the treeview.

                  如果有人在 C# 中有一个很好的例子,那就太好了.

                  If somebody has a good example in C# that would be great.

                  推荐答案

                  我已经有一段时间没有搞乱拖放了,所以我想我会写一个快速的示例.

                  It's been a while since I've messed with Drag/Drop so I figured I'll write a quick sample.

                  基本上,我有一个表单,左侧是列表框,右侧是树视图.然后我在上面放了一个按钮.单击按钮时,它只是将接下来十天的日期放入列表框中.它还使用 2 个父节点和两个子节点填充 TreeView.然后,您只需处理所有后续的拖放事件即可使其正常工作.

                  Basically, I have a form, with a listbox on the left, and a treeview on the right. Then I put a button on top. When the button is clicked, it just puts the date of the next ten days into the list box. It also populates the TreeView with 2 parents nodes and two child nodes. Then, you just have to handle all the subsequent drag/drop events to make it work.

                   public partial class Form1 : Form
                      {
                          public Form1()
                          {
                              InitializeComponent();
                              this.treeView1.AllowDrop = true;
                              this.listBox1.AllowDrop = true;
                              this.listBox1.MouseDown += new MouseEventHandler(listBox1_MouseDown);
                              this.listBox1.DragOver += new DragEventHandler(listBox1_DragOver);
                  
                              this.treeView1.DragEnter += new DragEventHandler(treeView1_DragEnter);
                              this.treeView1.DragDrop += new DragEventHandler(treeView1_DragDrop);
                  
                          }
                  
                          private void button1_Click(object sender, EventArgs e)
                          {
                              this.PopulateListBox();
                              this.PopulateTreeView();
                          }
                  
                          private void PopulateListBox()
                          {
                              for (int i = 0; i <= 10; i++)
                              {
                                  this.listBox1.Items.Add(DateTime.Now.AddDays(i));
                              }
                          }
                  
                          private void PopulateTreeView()
                          {
                              for (int i = 1; i <= 2; i++)
                              {
                                  TreeNode node = new TreeNode("Node" + i);
                                  for (int j = 1; j <= 2; j++)
                                  {
                                      node.Nodes.Add("SubNode" + j);
                                  }
                                  this.treeView1.Nodes.Add(node);
                              }
                          }
                  
                          private void treeView1_DragDrop(object sender, DragEventArgs e)
                          {
                  
                              TreeNode nodeToDropIn = this.treeView1.GetNodeAt(this.treeView1.PointToClient(new Point(e.X, e.Y)));
                              if (nodeToDropIn == null) { return; }
                              if(nodeToDropIn.Level > 0)
                              {
                                  nodeToDropIn = nodeToDropIn.Parent;
                              }
                  
                              object data = e.Data.GetData(typeof(DateTime));
                              if (data == null) { return; }
                              nodeToDropIn.Nodes.Add(data.ToString());
                              this.listBox1.Items.Remove(data);
                          }
                  
                          private void listBox1_DragOver(object sender, DragEventArgs e)
                          {
                              e.Effect = DragDropEffects.Move;
                          }
                  
                          private void treeView1_DragEnter(object sender, DragEventArgs e)
                          {
                              e.Effect = DragDropEffects.Move;
                          }
                  
                          private void listBox1_MouseDown(object sender, MouseEventArgs e)
                          {
                              this.listBox1.DoDragDrop(this.listBox1.SelectedItem, DragDropEffects.Move);
                          }
                  
                  
                      }
                  

                  这篇关于C# 拖动 &amp;从列表框拖放到树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么这个 Selenium 的拖放 C# 代码在 Chrome 上不起 下一篇:ItemsControl 拖放

                  相关文章

                  最新文章

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

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

                  1. <legend id='oEqnR'><style id='oEqnR'><dir id='oEqnR'><q id='oEqnR'></q></dir></style></legend>

                      <bdo id='oEqnR'></bdo><ul id='oEqnR'></ul>
                    <tfoot id='oEqnR'></tfoot>