Home>

In this article we share the code sharing function of item dragging in c#'s listbox.Have this

form1.cs code:

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)
    {
      //Single choice, multiple choices cannot be achieved
      //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 ();
    }
  }
}
c
  • Previous Python reptiles in action: analysis of "Wolf Warrior 2" Douban film review
  • Next Build reusable paging components with Vue