欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# ListBox中的Item拖拽代码分享

程序员文章站 2023-12-16 10:35:16
我们先来看下运行效果图 form1.cs代码: using system; using system.collections.generic; usin...

我们先来看下运行效果图

C# ListBox中的Item拖拽代码分享

form1.cs代码:

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.threading.tasks;
using system.windows.forms;
using system.collections;

namespace moveitem
{
  public partial class form1 : form
  {
    public form1()
    {
      initializecomponent();
    }
    arraylist list = new arraylist();
    private void listbox1_selectedindexchanged(object sender, eventargs e)
    {

    }

    private void form1_load(object sender, eventargs e)
    {
      for (int i = 1; i <= 10; i++)
      {
        list.add(i);
        string s = i.tostring();
        listbox1.items.add(s);
      }

    }

    private void listbox2_selectedindexchanged(object sender, eventargs e)
    {
      
    }

    private void button1_click(object sender, eventargs e)
    {
      //单选,无法实现多选
      //string str = this.listbox1.text.trim().tostring();
      //if (listbox1.items.contains(str))
      //{
      //  listbox1.items.remove(str);
      //  listbox2.items.add(str);
      //}
      for (int i=0;i<listbox1.selectedindices.count;i++)
      {
        listbox2.items.add(listbox1.items[listbox1.selectedindices[i]]);
        listbox1.items.removeat(listbox1.selectedindices[i]);
        i--;
      }
    }

    private void button2_click(object sender, eventargs e)
    {
      for (int i = 0; i < listbox2.selectedindices.count; i++)
      {
        listbox1.items.add(listbox2.items[listbox2.selectedindices[i]]);
        listbox2.items.removeat(listbox2.selectedindices[i]);
        i--;
      }
    }

    private void button3_click(object sender, eventargs e)
    {
      listbox2.items.addrange(listbox1.items);
      listbox1.items.clear();
    }

    private void button4_click(object sender, eventargs e)
    {
      listbox1.items.addrange(listbox2.items);
      listbox2.items.clear();
    }
  }
}

大家可以测试运行下,有其他问题可以在下方的留言区讨论,感谢大家对的支持。

上一篇:

下一篇: