两个LISTBOX控件,要求两个LISTBOX中的项可以左右移动,可单个移动也可多个移动。点击右移LISTBOX_1的值传到LISTBOX_2里。点左一样。
 怎么实现呀

解决方案 »

  1.   

       按钮事件!     添加list和删除list!
      

  2.   

    private void button1_Click(object sender, EventArgs e) 
            { 
              
                if (this.listBox1.SelectedIndex > -1) 
                { 
                    this.listBox2.Items.Add(listBox1.SelectedItem); 
                    this.listBox1.Items.Remove(listBox1.SelectedItem); 
                } 
                Set(); 
            } 
            private void button2_Click(object sender, EventArgs e) 
            { 
                if (this.listBox2.SelectedIndex > -1) 
                { 
                    this.listBox1.Items.Add(listBox2.SelectedItem); 
                    this.listBox2.Items.Remove(listBox2.SelectedItem); 
                } 
                Set(); 
            } 
            private void Set() 
            { 
                this.button1.Enabled = this.listBox1.Items.Count>0 ? true : false; 
                this.button2.Enabled = this.listBox2.Items.Count > 0 ? true : false; 
            } 
    大体一致
      

  3.   

       //选择向右移: 
          int Count = ListBox1.Items.Count;        for (int Index = 0; Index < Count; Index++)
            {
                ListItem Item = ListBox1.Items[Index];            if (ListBox1.Items[Index].Selected == true)
                {
                    ListBox1.Items.Remove(Item);
                    ListBox2.Items.Add(Item);                Count--;
                    Index--;
                }
            } 
     //选择向左移代码:
          int Count = ListBox2.Items.Count;        for (int Index = 0; Index < Count; Index++)
            {
                ListItem Item = ListBox2.Items[Index];            if (ListBox2.Items[Index].Selected == true)
                {
                    ListBox2.Items.Remove(Item);
                    ListBox1.Items.Add(Item);                Count--;
                    Index--;
                }
            }
      

  4.   


    另外ListBox1需要首先赋值测试:
    <asp:ListBox ID="ListBox1" runat="server" Height="156px" Width="132px" SelectionMode="Multiple">
        <asp:ListItem>轻音乐</asp:ListItem>
        <asp:ListItem>热门DJ</asp:ListItem>
        <asp:ListItem>伤感音乐专题</asp:ListItem>
    </asp:ListBox>另外增加连个按钮:右移按钮: btnMoveRight,事件如上代码,左移按钮:btnMoveLeft
      

  5.   

    单个移动:
        自己去删除和添加items就可以了;
    多个移动:
        首先要实现选择多项,想怎么选自己去定义,然后把这些项逐一添加,最后删除原来的。具体代码楼上几位贴了不少,看看大概就可以了。
      

  6.   

    int Count = ListBox1.Items.Count;        for (int Index = 0; Index < Count; Index++)
            {
                ListItem Item = ListBox1.Items[Index];            if (Item.Selected == true)
                {
                    ListBox1.Items.Remove(Item);
                    ListBox2.Items.Add(Item);                Count--;
                    Index--;
                }
            }同理就可以实现了