【发布时间】:2011-08-24 14:48:10
【问题描述】:
我试图在这些行中实现一些东西,我有两个列表框 listbox1 和 listbox2,我分别有添加和删除按钮来将项目从一个列表框移动到另一个。由于某种原因,我无法选择该项目几次移动后从列表框中。
我正在使用以下代码
添加按钮的逻辑
ArrayList listitems1 = new ArrayList();
ArrayList listitems2 = new ArrayList();
if (listBox2.SelectedItem == null)
{
System.Windows.MessageBox.Show("*Please Select unassigned Wks ");
return;
}
else
{
try
{
foreach (string st in listBox2.Items)
{
listitems1.Add(st);
}
foreach (string st1 in listBox1.Items)
{
listitems2.Add(st1);
}
if (listBox2.SelectedIndex > -1)
{
string value = listBox2.SelectedItem.ToString();
string text = listBox2.SelectedItem.ToString();
listitems2.Add(value);
listBox1.ItemsSource = listitems2;
listitems1.Remove(value);
listBox2.ItemsSource = listitems1;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
**logic for delete button:**
ArrayList listitems1 = new ArrayList();
ArrayList listitems2 = new ArrayList();
if (listBox2.SelectedItem == null)
{
System.Windows.MessageBox.Show("*Please Select unassigned Wks ");
return;
}
else
{
try
{
foreach (string st in listBox2.Items)
{
listitems1.Add(st);
}
foreach (string st1 in listBox1.Items)
{
listitems2.Add(st1);
}
if (listBox2.SelectedIndex > -1)
{
string value = listBox2.SelectedItem.ToString();
string text = listBox2.SelectedItem.ToString();
listitems2.Add(value);
listBox1.ItemsSource = listitems2;
listitems1.Remove(value);
listBox2.ItemsSource = listitems1;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
ArrayList listitems1 = new ArrayList();
ArrayList listitems2 = new ArrayList();
try
{
foreach (string st in listBox2.Items)
{
listitems1.Add(st);
}
foreach (string st1 in listBox1.Items)
{
listitems2.Add(st1);
}
if (listBox1.SelectedIndex > -1)
{
string value = listBox1.SelectedItem.ToString();
string text = listBox1.SelectedItem.ToString();
listitems1.Add(value);
listBox2.ItemsSource = listitems1;
listitems2.Remove(value);
listBox1.ItemsSource = listitems2;
}
}
【问题讨论】:
-
当
listitems2和listitems1超出范围时,我不确定它们作为ListBoxes 的来源是否有效