• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请



 定义多个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:[email protected],[email protected] 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:[email protected],[email protected] 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)
        {

 

       }


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#获取存储过程返回的参数Output发布时间:2022-07-10
下一篇:
C#将Excel表格中的数据导入SQL数据库发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap