locked
webform: how to move items from listbox to another listbox? RRS feed

  • Question

  • Hi, unlike the other thread, my question is regarding dealing with webforms using C#. 

     I have 2 listboxs in webform

    is it possible let items transfer between the 2 listboxes?

    for example:

    listbox1 have 2 items : Joan and ViVi

    listbox2 have 1 item : Kevin

    if I choose Joan from listbox1 and click bottn

    Joan will disapper from listbox1 and appear on listbox2

    so that I have this result after click bottn

    listbox1 have 1 item: ViVi

    listbox2 have 2 item : Kevin and Joan

    I would like to have mutiple selection enabled too. 

    Thanks

    Friday, March 23, 2012 8:40 PM

Answers

All replies

    • Marked as answer by smetah Friday, March 23, 2012 11:39 PM
    • Unmarked as answer by smetah Monday, March 26, 2012 1:07 PM
    • Marked as answer by smetah Monday, April 2, 2012 11:09 PM
    Friday, March 23, 2012 9:13 PM
  • I would like to have mutiple selectionenabled too. 

    To move multiple items?

    Anyway, you can do it like:

    private void button1_Click(object sender, EventArgs e)
    {
        List<string> items = new List<string>();
        if(listBox1.Focused)
        {
            for(int i= 0; i < listBox1.SelectedItems.Count; i++)
                  items.Add(listBox1.SelectedItems[i].ToString());
        }
        else
        {
            for(int i= 0; i < listBox2.SelectedItems.Count; i++)
                 items.Add(listBox1.SelectedItems[i].ToString());       
        }
        foreach(string item in items)
        {
            if(listBox1.Focused)
            {
                listBox2.Items.Add(item);
                listBox1.Items.Remove(item);
            }
            else
            {
                listBox1.Items.Add(item);
                listBox2.Items.Remove(item);
            }
        }
    }


    Mitja

    • Marked as answer by smetah Friday, March 23, 2012 11:40 PM
    • Unmarked as answer by smetah Monday, March 26, 2012 1:07 PM
    Friday, March 23, 2012 9:38 PM
  • Thanks everyone.

    Friday, March 23, 2012 11:40 PM
  • @Marco, Thanks, but I need handle mutiple selection.

    @Mitja, This is what I got:

    'System.Web.UI.WebControls.ListBox' does not contain a definition for 'SelectedItems' and no extension method 'SelectedItems' accepting a first argument of type 'System.Web.UI.WebControls.ListBox' could be found

    Once again, my question is regarding dealing with webforms using C#. So far all "solutions" that I found are for window forms, which have incompatible method to port to webforms. 

    Please help.

    Thanks

     

    Monday, March 26, 2012 1:11 PM
  • Oh, found it. 

    Just change the if to while, 

    protected void btnRemove_Click(object sender, EventArgs e)
        {
            while (lstSelectedEmployees.SelectedIndex > -1)
            {
                string _value = lstSelectedEmployees.SelectedItem.Value; //Gets the value of items in list.
                string _text = lstSelectedEmployees.SelectedItem.Text;  // Gets the Text of items in the list.  
                ListItem item = new ListItem(); //create a list item
                item.Text = _text;               //Assign the values to list item   
                item.Value = _value;
                lstSelectedEmployees.Items.Remove(item); //Remove from the selected list
                lstEmployees.Items.Add(item); //Add in the Employee list 
    
            }
    
        }
    

    Thanks everyone!

    Monday, March 26, 2012 2:10 PM
  • you know you can remove an item by index.  You don't need to create a list item and populate it's text and value just to remove the selected item(s).
    Monday, March 26, 2012 3:48 PM
  • @servy42, care to illustrate by code?

    Thanks

    Monday, March 26, 2012 4:25 PM
  • foreach(int i in listBox.GetSelectedIndecies().Reverse())
    {
      listBox.Items.RemoveAt(i);
    }

    The reverse is important; you need to start and the end and work backwards so that removing one item doesn't change the index of the other items to remove.

    Or just:

    while(listBox.SelectedIndex > -1)
    {
      listBox.RemoveAt(listBox.SelectedIndex);
    }

    if you want something that looks closer to the code that you have.

    • Edited by servy42 Monday, March 26, 2012 4:51 PM
    Monday, March 26, 2012 4:49 PM
  • Thanks a lot for your code, servy42.

    I guess the reason that the code went the whole 9 yards was to take care of adding as well (and adding in reverse order will not be too comfortable for most people).

    Thanks anyway. 

    Monday, March 26, 2012 4:54 PM
  • Thanks a lot for your code, servy42.

    I guess the reason that the code went the whole 9 yards was to take care of adding as well (and adding in reverse order will not be too comfortable for most people).

    Thanks anyway. 

    Do all your adding before you do any removing.

    foreach(int i in listBox.SelectedIndecies())
    {
      listBox3.Items.Add(listBox.Items[i]);
    }
    At that point it doesn't matter if they are removed forwards or backwards.


    • Edited by servy42 Monday, March 26, 2012 5:07 PM
    Monday, March 26, 2012 5:07 PM
  • hmm.. that need two loops.

    I'll see what I can do ... 

    Monday, March 26, 2012 5:11 PM
  • Here it is,

    while(listBox.SelectedIndex > -1) {

    listBox3.Items.Add(listBox.Items[listBox.SelectedIndex]);

    listBox.RemoveAt(listBox.SelectedIndex); }

    Should be the fastest implementation. 

    • Marked as answer by smetah Monday, April 2, 2012 11:09 PM
    Monday, March 26, 2012 5:16 PM
  • The amount of time it takes to loop through a few hundred thousand items is going to be less than a couple of milliseconds.  I'm sure it can loop through your handful of items in substantially less than a millisecond.

    Combining the two loops saves you virtually nothing you could ever measure.

    Monday, March 26, 2012 5:20 PM
  • hmm... nowadays people don't care about "do it right" any more?

    I'm too old. ;-)

    Monday, March 26, 2012 5:22 PM
  • hmm... nowadays people don't care about "do it right" any more?

    I'm too old. ;-)

    No, my point is that combining the loops isn't "doing it right".

    There is nothing in the least bit wrong with using two different loops.  

    The underlying concept here is that you should not prematurely optimize.  If your code is running too slowly profile it, see where the problem areas are, and then focus on optimizing that.  When you attempt to make micro-optimizations all over the place, especially when your code is already more than fast enough, you increase the probability of errors for no actual gains.

    Any time you're dealing with code snippets that you KNOW are going to run extraordinarily quickly you should emphasize code readability and obvious correctness, and shouldn't really consider performance at all.  When you're writing code snippets that you know will significantly impact the overall performance of your application (such as the database queries used) then it's worth the time and effort to consider the performance implications and potentially increase the complexity of the app to improve performance.

    Monday, March 26, 2012 5:29 PM