.net C# listbox的上下移動,拖動排序,兩個listbox相互拖動

來源:互聯網
上載者:User

 定義多個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,對本文的作者表示由衷感謝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.