海海日记-冯海滨博客

您现在的位置是:首页 > C# > 正文

C#

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

happyfhb2013-12-31C#1857
//定义多个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);
        }


平淡中储蓄成长

发表评论

评论列表

  • 这篇文章还没有收到评论,赶紧来抢沙发吧~