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]);
}
}
}
本文介绍了一种在C#中实现两个ListBox间数据互相转移的方法。通过按钮点击事件触发,选择ListBox1中的项并移至ListBox2,再从ListBox2选中项返回ListBox1,实现了双向的数据交换。

2064

被折叠的 条评论
为什么被折叠?



