我有两个列表框L1和L2。现在在按钮单击方法上,我必须将L1中的选定项目移动到L2,并且L1中的项目应该被删除。
protected void Btn2_Click(object sender, EventArgs e)
{
string sel = LB1.SelectedValue;
List<string> ab = new List<string>();
ab.Add(sel);
L2.Text = Convert.ToString(ab.Count);
for(int i =0; i < ab.Count ; i++)
{
string c = ab[i];
LB2.Items.Add(c);
}
答案 0 :(得分:0)
如果您要从SelectedItem
ListBox1
在函数末尾添加以下语句:
LB1.Items.Remove(LB1.SelectedValue);
完整代码:
protected void Btn2_Click(object sender, EventArgs e)
{
string sel = LB1.SelectedValue;
List<string> ab = new List<string>();
ab.Add(sel);
L2.Text = Convert.ToString(ab.Count);
for(int i =0; i < ab.Count ; i++)
{
string c = ab[i];
LB2.Items.Add(c);
}
LB1.Items.Remove(LB1.SelectedValue);//Add This to remove selected item from ListBox1
}
答案 1 :(得分:-1)
protected void Btn2_Click(object sender, EventArgs e)
{
List<ListItem> itemList = new List<ListItem>();
if (LB1.SelectedIndex >= 0)
{
for (int i = 0; i < LB1.Items.Count; i++)
{
if (LB1.Items[i].Selected)
{
if (!itemList.Contains(LB1.Items[i]))
{
itemList.Add(LB1.Items[i]);
}
}
}
for (int i = 0; i < itemList.Count; i++)
{
if (!LB2.Items.Contains(itemList[i]))
{
LB2.Items.Add(itemList[i]);
}
LB1.Items.Remove(itemList[i]);
}
LB2.SelectedIndex = -1;
}
}