yanleigis的专栏

闫磊 GIS

.net 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,对本文的作者表示由衷感谢

阅读更多
个人分类: C#
想对作者说点什么? 我来说一句

上下左右移动选择的布局

PsEmperor PsEmperor

2016-08-18 14:50:10

阅读数:436

.net 实现常用经典排序算法

2010年09月18日 41KB 下载

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭