Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i know how to transfer or pass value from one Listbox to Another Listbox
but i Don't know how transfer it without selection

What I have tried:

listBox2.Items.Add(listBox1.SelectedItem);
listBox1.Items.Remove(listBox1.SelectedItem);
this i tired but i select the items but i want to without selecting, transfer them
Posted
Updated 9-Jan-17 21:36pm

The SelectedItem property just returns the currently selected item - but you are already using the property you need: ListBox.Items
All you have to do is decide which item(s) you want, and access them directly via the Items property:
listBox2.Items.Add(listBox1.Items[indexOfItemIWant]);
listBox1.Items.Remove(listBox1.Items[indexOfItemIWant]);
 
Share this answer
 
It's basically the same, but you just define the index of the item you want to modify.

Consider the following example
C#
ListBox lb1 = new ListBox();
ListBox lb2 = new ListBox();

lb1.Items.Add("first");
lb1.Items.Add("second");
lb1.Items.Add("third");

// move the second item
lb2.Items.Add(lb1.Items[1]);
lb1.Items.RemoveAt(1);

[ADDED]

Example of moving several items
C#
// move two items items 2 and 0
// remember that when using indexes loop from biggest to smallest
foreach (int index in new int[] { 2, 0 }) {
   lb2.Items.Add(lb1.Items[index]);
   lb1.Items.RemoveAt(index);
}

[ADDED]
Since you're using multiselect list as source, you can loop through the selected items. For example
C#
ListBox lb1 = new ListBox();
ListBox lb2 = new ListBox();

lb1.Items.Add("first");
lb1.Items.Add("second");
lb1.Items.Add("third");

// select items 0 and 2
lb1.SelectionMode = SelectionMode.MultiSimple;
lb1.SelectedItems.Add(lb1.Items[0]);
lb1.SelectedItems.Add(lb1.Items[2]);

// move the selected items
for (int counter = lb1.SelectedItems.Count - 1; counter >= 0; counter-- ) {
   lb2.Items.Add(lb1.SelectedItems[counter]);
   lb1.Items.Remove(lb1.SelectedItems[counter]);
}
 
Share this answer
 
v4
Comments
Bakhshi-faisal 10-Jan-17 3:40am    
IT doesn't work
Wendelius 10-Jan-17 3:43am    
Why not? Of course you need to use your real list boxes instead of the lb1 and lb2. Those were created for demonstration purposes only.

If you run the code above and in the debugger examine the items in lb1 and lb2 you see that after running the code lb1 contains 2 items and lb2 contains 1 item.
Bakhshi-faisal 10-Jan-17 3:51am    
thank you very much
it works but for passing multiple value without selecting them what should we add in the code
Wendelius 10-Jan-17 4:11am    
As you can see in the code example I didn't select anything from the listbox nor did I use SelectedItem.

So to answer the question you need to make decision, what item indexes you move and then use the move code above in a loop where you replace 1 with the counter for the loop
Wendelius 10-Jan-17 4:16am    
Added a new example

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900