2014-02-17 72 views
0

我有兩個列表框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

nope,當listbox1移動到listbox2時,它們應該被刪除。 – Shrugo

回答

0

,如果你想從ListBox1

刪除SelectedItem在你的函數的末尾添加以下語句:

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
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; 
    } 
} 
相關問題