Click here to Skip to main content
15,072,749 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]);
   
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]);
}
   
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
Bakhshi-faisal 10-Jan-17 4:20am
   
i got it thank you
Wendelius 10-Jan-17 4:26am
   
You're most welcome :)
Bakhshi-faisal 10-Jan-17 4:39am
   
sorry
but still don't work
i don't know this is for passing single value

if (listBox1.SelectedIndex == -1)
{
MessageBox.Show(" Veuillez sélectionner un choix");
}
else
{
listBox2.Items.Add(listBox1.SelectedItem);
listBox1.Items.Remove(listBox1.SelectedItem);
but for the mulitple
if (listBox1.SelectedIndex == -1)
{
MessageBox.Show(" Veuillez sélectionner un choix");
}
while(listBox1.SelectedItems.Count !=0)
{
listBox2.Items.Add(listBox1.SelectedItems[0]);
listBox1.Items.Remove(listBox1.SelectedItems[0]);
}
i tried this but i should select the items is there any solution without selecting, pass them
Wendelius 10-Jan-17 4:54am
   
See the updated answer
Bakhshi-faisal 10-Jan-17 4:55am
   
thanks it works perfectly
Bakhshi-faisal 10-Jan-17 5:04am
   
i can display one item when i select it
but i can't display all items at once

What I have tried:

i tried this
form1
private void btn_voit_Click(object sender, EventArgs e)
{
string textboxchoix = listBox2.Text;
Form2 frm = new Form2(textboxchoix);
frm.Show();
}
form2
public Form2(string textboxchoix)
{
InitializeComponent();
label_choix.Text = textboxchoix;
Bakhshi-faisal 10-Jan-17 5:05am
   
i dont know how to display all items of a textBox in a label
when i tried i display only one that i selected
Wendelius 10-Jan-17 5:20am
   
I see you have already opened a new question for this. That's good
Bakhshi-faisal 10-Jan-17 5:27am
   
ok can you help me to figure it out

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