PDA

Click to See Complete Forum and Search --> : multiple selection in a list box


steve_rm
Oct 12th, 2003, 06:34 AM
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



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

dynamic_sysop
Oct 12th, 2003, 07:20 AM
for the listbox question ...

// add the items
private void button1_Click(object sender, System.EventArgs e)
{
string[] li = {"item 1","item 2","item 3" , "item 4"};
listBox1.Items.AddRange(li); // adds the group of items to the listbox in 1 go.
}

// then to remove the items...
private void button2_Click(object sender, System.EventArgs e)
{
string[] li = {"item 1","item 2","item 3" , "item 4"};
foreach(string s in li)
{
listBox1.Items.Remove(s);
}
}

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.

DevGrp
Oct 12th, 2003, 09:29 AM
Here is another example

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);
}

PT Exorcist
Oct 14th, 2003, 06:54 AM
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.......:rolleyes: