Results 1 to 4 of 4

Thread: multiple selection in a list box

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    multiple selection in a list box

    Hello

    I am using the code below to add multiple items from a list box called lstNamesToAdd to another list box called lstNames.

    I click multiple items and then click a button to add them. The code does not work. Can anyone tell me the best way to this

    Code:
    lstName.Items.Add(lstNameToAdd.SelectItems);
    lstNamesToAdd.Items.Remove(lstNamesToAdd.SelectedItems);
    Question 2
    Does C# use a inputbox to accept input from a user?

    Many thanks in advance

    Steve
    steve

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    for the listbox question ...
    VB Code:
    1. [color=green]// add the items[/color]
    2.         [color=blue]private void[/color] button1_Click([color=blue]object[/color] sender, System.EventArgs e)
    3.         {
    4.             [color=blue]string[/color][] li = {"item 1","item 2","item 3" , "item 4"};
    5.             listBox1.Items.AddRange(li); [color=green]//  adds the group of items to the listbox in 1 go.[/color]
    6.         }
    7.  
    8. [color=green]// then to remove the items...[/color]
    9.         [color=blue]private void[/color] button2_Click([color=blue]object[/color] sender, System.EventArgs e)
    10.         {
    11.             [color=blue]string[/color][] li = {"item 1","item 2","item 3" , "item 4"};
    12.             [color=blue]foreach[/color]([color=blue]string[/color] s [color=blue]in[/color] li)
    13.             {
    14.                 listBox1.Items.Remove(s);
    15.             }
    16.         }
    you shouldnt use InputBox's ( as in the vb6 type ) in .Net, the best way is to create your own custom boxes ( with Forms )
    hope this helps.
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Here is another example
    Code:
    string[] itemsToRemove = new string[lstNamesToAdd.SelectedItems.Count];
    lstNamesToAdd.SelectedItems.CopyTo(itemsToRemove, 0);
    								
    foreach(string item in lstNamesToAdd.SelectedItems)
    {
    	lstNAme.Items.Add(item);
    }
    
    foreach(string item in itemsToRemove)
    {
    	lstNamesToAdd.Items.Remove(item);
    }

  4. #4
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    you shouldnt use InputBox's ( as in the vb6 type ) in .Net, the best way is to create your own custom boxes ( with Forms )
    hope this helps.
    anyways they were so ugly looking.......
    \m/\m/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width