首页 > 代码库 > ListBox

ListBox

//左到右

private void btnLeftToRight_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItems != null)
{
foreach (var item in listBox1.SelectedItems)
{
listBox2.Items.Add(item.ToString());
}

for (int i = 0; i < listBox1.SelectedIndices.Count; i++)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndices[i]);
i--;
}
}
}

 

//右到左

private void btnRightToLeft_Click(object sender, EventArgs e)
{
if (listBox2.SelectedItems != null)
{
foreach (var item in listBox2.SelectedItems)
{
listBox1.Items.Add(item.ToString());
}

for (int i = 0; i < listBox2.SelectedIndices.Count; i++)
{
listBox2.Items.RemoveAt(listBox2.SelectedIndices[i]);
i--;
}
}
}

 

追问:

foreach (var item in listBox1.SelectedItems)
{
listBox2.Items.Add(item.ToString());
}

for (int i = 0; i < listBox1.SelectedIndices.Count; i++)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndices[i]);
i--;
}

ListBox