Click here to Skip to main content
15,868,440 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi every body!

I have 3 ListBox(s) and i want if user choose third line of second ListBox in other ListBoxs third line choose itself.

what should i do to do this?

I mean: 1.How can i understand the selected item in a ListBox and get it and 2.How can i select an item in a ListBox


Thanks

Improved: (Best Answer)

first i should say thanks to s.b who help me,

That thing i understand from the solutions : We have 2 (best , easy) way to do it 1. We can go to the listBox1_SelectedIndexChanged and write there int A = listBox1.SelectedIndex; that A is the line of listBox1 selected and set it in listBox2 2. You can write the lines of every listbox at first of every line and after that you can get the number of the line you select and find it in another list box. (My self use this one)

Good Luck
Posted
Updated 28-Jun-11 0:57am
v3

C#
int sel = myListBox2.SelectedIndex; // get the index of the current selected item
if ( sel < myListBox1.Items.Count) // 'cautious programmer' check
  myListBox1.SelectedIndex = sel; // set the index of the selected item (i.e. force selection to index 'sel')
if ( sel < myListBox3.Items.Count) 
  myListBox3.SelectedIndex = sel;  
 
Share this answer
 
v3
Comments
#realJSOP 25-Jun-11 6:18am    
5 - but of course, the cautious programmer would check the listbox to make sure "sel" is not out of range before selecting it. :)
CPallini 25-Jun-11 6:27am    
Fixed, thanks.
The SelectedItem[^] property tells you what item has been selected by the user. You can use the same property to set the selection to a specific item yourself.
 
Share this answer
 
Comments
Sander Rossel 25-Jun-11 6:21am    
Good, but not entirely complete. See my additions :)
Eddy Vluggen 25-Jun-11 14:52pm    
Hehe, true, but there's a link to the documentation; just a matter of investing the time to read it. Good additions btw, should have enough pointers to get started :)
Just adding to Eddy Vluggens answer, to check when a user clicks a certain item you can use the SelectedIndexChanged or SelectedValueChanged Event of the ListBox. You could then check the SelectedItem, SelectedIndex or SelectValue.
As Eddy already pointed out you can then set the SelectedItem, SelectedIndex or SelectValue of your second ListBox.

An example on MSDN[^]. Good luck!
 
Share this answer
 
the selectitem property of the list box will suffice ur answer
also use you can make use of selectindex property of the listbox that gives ur selections based in indexes ,,,.... remember index starts with 0
 
Share this answer
 

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