C# listbox的上下移动,拖动排序,两个listbox相互拖动



 定义多个Listbox,可以实现相互拖动,如listbox1,listbox2,设置如下allowdrop=true和

this.listBox2.DragDrop += new System.Windows.Forms.DragEventHandler(this.ListBox1_DragDrop);
            this.listBox2.MouseDown += new System.Windows.Forms.MouseEventHandler

(this.ListBox1_MouseDown);
            this.listBox2.DragOver += new System.Windows.Forms.DragEventHandler

(this.ListBox1_DragOver);

 
  private void moveUpListBox(ListBox ListBox1) //向上移动
        {
            //by 闫磊 Email:Landgis@126.com,yanleigis@21cn.com 2007.10.11
            //若不是第一行则上移
            if (ListBox1.SelectedIndex > 0)
            {

                int index = ListBox1.SelectedIndex;
                string temp = ListBox1.Items[index - 1].ToString();
                ListBox1.Items[index - 1] = ListBox1.SelectedItem.ToString(); ;
                ListBox1.Items[index] = temp;
                ListBox1.SelectedIndex = index - 1;


            }
        }
        private void moveDownListBox(ListBox ListBox1)  /**/////向下移动
        {
            //若不是第最后一行则下移
            if (ListBox1.SelectedIndex < ListBox1.Items.Count - 1)
            {

                int index = ListBox1.SelectedIndex;
                string temp = ListBox1.Items[index + 1].ToString();
                ListBox1.Items[index + 1] = ListBox1.SelectedItem.ToString(); ;
                ListBox1.Items[index] = temp;
                ListBox1.SelectedIndex = index + 1;
                //ListBox1.val



            }
        }
        private void button1_Click(object sender, EventArgs e) //调用向上移动
        {
            moveUpListBox(ListBox1);

        }

        private void button2_Click(object sender, EventArgs e) //调用向下移动
        {
            moveUpListBox(ListBox1);
        }
       //下面是拖动
      //by 闫磊 Email:Landgis@126.com,yanleigis@21cn.com 2007.10.11

        private ListBox sourcelbl;//拖动源


        private void ListBox1_DragOver(object sender, DragEventArgs e)
        {

            //拖动源和放置的目的地一定是一个ListBox
            if (e.Data.GetDataPresent(typeof(System.String)))
            {
                e.Effect = DragDropEffects.Move;
            }
            else
                e.Effect = DragDropEffects.None;

        }

        private void ListBox1_DragDrop(object sender, DragEventArgs e)
        {


            ListBox listbox = (ListBox)sender;
            int indexoftarget = listbox.IndexFromPoint(listbox.PointToClient(new Point(e.X, e.Y)));

            //拖动源和放置的目的地可能不一样
            int idx = -1;
            for (int i = sourcelbl.SelectedItems.Count - 1; i == 0; i--)
            {
                if (!listbox.Equals(sourcelbl) && indexoftarget == -1)
                {
                    idx = listbox.Items.Add(sourcelbl.SelectedItems[i].ToString());

                }
                else
                {
                    listbox.Items.Insert(indexoftarget, sourcelbl.SelectedItems[i].ToString());


                }

            }
            sourcelbl.Items.RemoveAt(sourcelbl.SelectedIndex);

            if (idx > -1)
            {
                listbox.SelectedIndex = idx;
            }
            else
                listbox.SelectedIndex = indexoftarget;

        }


        private void ListBox1_MouseDown(object sender, MouseEventArgs e)
        {
            sourcelbl = (ListBox)sender;
            int indexofsource = sourcelbl.SelectedIndex;
            if (indexofsource > -1)
            {
                sourcelbl.DoDragDrop(sourcelbl.Items[indexofsource].ToString(), DragDropEffects.All);

            }

        }

 

//本文参考:http://blog.csdn.net/J_S_S/archive/2007/06/02/1635012.aspx,对本文的作者表示由衷感谢

//////////////////////////////////////////

实现listbox 之间的自由拖拽源代码


 public static int n;
        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            int i = 0;
            for (i = 0; i <= listBox1.Items.Count - 1; i++)
            {
                if (listBox1.GetSelected(i))
                {
                    n = i;
                    break;
                }
            }
        }
        private void listBox2_MouseHover(object sender, EventArgs e)
        {
            bool flag = false;
            for (int i = 0; i <= listBox2.Items.Count - 1; i++)
            {
                if (listBox1.Items[n].ToString() == listBox2.Items[i].ToString())
                {
                    flag = true;
                    break;
                }
            }
            if (flag)
            {
                MessageBox.Show("对不起,移动错误!");
            }
            else
            {
                listBox2.Items.Add(listBox1.Items[n].ToString());
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

 

       }

原文地址:https://www.cnblogs.com/furenjian/p/3044068.html