C# ListBox数据互换

来源:互联网 发布:经典网络歌曲打包下载 编辑:程序博客网 时间:2024/05/29 18:15

C#中两个ListBox数据互换的逻辑,下面为一段实例code,供参考。

private void btn2Right_Click(object sender, System.EventArgs e)
  {
   
   //Add item from ListBox1
   for (int i = 0; i < this.ListBox1.Items.Count ; i++)
   {
    if(ListBox1.Items[i].Selected)
    { 
     this.ListBox2.Items.Add(new ListItem(ListBox1.Items[i].Text,ListBox1.Items[i].Value));
    }
   } 
  
   //Delete item from ListBox1, when it add item to ListBox2 successful
   for (int i = this.ListBox1.Items.Count -1; i >=0  ; i--)
   {
    if(ListBox1.Items[i].Selected)
    {
     this.ListBox1.Items.Remove(ListBox1.Items[i]);
    }
   } 
  }

  private void btn2Left_Click(object sender, System.EventArgs e)
  {
   //Add item from ListBox2
   for (int i = 0; i < this.ListBox2.Items.Count ; i++)
   {
    if(ListBox2.Items[i].Selected)
    { 
     this.ListBox1.Items.Add(new ListItem(ListBox2.Items[i].Text,ListBox2.Items[i].Value));
    }
   } 
   //Delete item from ListBox2, when it add item to ListBox1 successful
   for (int i = this.ListBox2.Items.Count -1; i >=0  ; i--)
   {
    if(ListBox2.Items[i].Selected)
    {
     this.ListBox2.Items.Remove(ListBox2.Items[i]);
    }
   }  
  } 

原创粉丝点击