Multiple select in listbox
-
Hi I'm building a little "search" function for my listbox! This is what i want to happend: I got a textbox, then i write the number (the listbox contains a list of numbers) i want to find. The number i write in the textbox should then get selected in the listbox when i click a button. That far, it works great with this code:
lstlistBox1.SelectedItem = txttextBox1.Text;
Now to the problem. If the number i write in the textbox exists more than one time in the list, i want all the items with that name to get selected. Been trying with loops and stuff to make it happend but just wont work. Anyone got a suggestion? Thx in advanceLight is faster than sound... Thats why some people look smart till they start to talk.
-
Hi I'm building a little "search" function for my listbox! This is what i want to happend: I got a textbox, then i write the number (the listbox contains a list of numbers) i want to find. The number i write in the textbox should then get selected in the listbox when i click a button. That far, it works great with this code:
lstlistBox1.SelectedItem = txttextBox1.Text;
Now to the problem. If the number i write in the textbox exists more than one time in the list, i want all the items with that name to get selected. Been trying with loops and stuff to make it happend but just wont work. Anyone got a suggestion? Thx in advanceLight is faster than sound... Thats why some people look smart till they start to talk.
Iterate over the listbox items and use the SetSelect method to select or deselect the items according to the number in the textbox.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
Iterate over the listbox items and use the SetSelect method to select or deselect the items according to the number in the textbox.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
Hi I'm building a little "search" function for my listbox! This is what i want to happend: I got a textbox, then i write the number (the listbox contains a list of numbers) i want to find. The number i write in the textbox should then get selected in the listbox when i click a button. That far, it works great with this code:
lstlistBox1.SelectedItem = txttextBox1.Text;
Now to the problem. If the number i write in the textbox exists more than one time in the list, i want all the items with that name to get selected. Been trying with loops and stuff to make it happend but just wont work. Anyone got a suggestion? Thx in advanceLight is faster than sound... Thats why some people look smart till they start to talk.
Hai do the following: //Add a button and add the following code to that { listBox1.SelectionMode = SelectionMode.MultiExtended; string strSearch = textBox1.Text; for (int i = 0,j=0; i < listBox1.Items.Count; i++) { if (listBox1.Items[i].ToString() == strSearch) listBox1.SetSelected(i, true); else listBox1.SetSelected(i, false); } } Add a textbox and a listbox and try to execute. regards,
VijayaRam
-
Hai do the following: //Add a button and add the following code to that { listBox1.SelectionMode = SelectionMode.MultiExtended; string strSearch = textBox1.Text; for (int i = 0,j=0; i < listBox1.Items.Count; i++) { if (listBox1.Items[i].ToString() == strSearch) listBox1.SetSelected(i, true); else listBox1.SetSelected(i, false); } } Add a textbox and a listbox and try to execute. regards,
VijayaRam
Good one, worked on first try :-D Thanks! Next part of the application is to make another listbox select the same indexes as the first one does, heres also the problem that it only selects one. (note: the same indexes, not the same numbers :)) Any suggestions?
Light is faster than sound... Thats why some people look smart till they start to talk.
-
Good one, worked on first try :-D Thanks! Next part of the application is to make another listbox select the same indexes as the first one does, heres also the problem that it only selects one. (note: the same indexes, not the same numbers :)) Any suggestions?
Light is faster than sound... Thats why some people look smart till they start to talk.
Did you even try to understand why and how the code snippet you were given solved your original problem? I doubt that cause otherwise, you would know how to easily solve this one.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
Did you even try to understand why and how the code snippet you were given solved your original problem? I doubt that cause otherwise, you would know how to easily solve this one.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
Martin# wrote:
Yes, a "make my homework, so I don't have to think!" message board is missing!
Not asking anyone to "make my homework", i simply couldn't get it working... Tried understanding it, tried to get it work and i couldn't, thats why i asked.
Light is faster than sound... Thats why some people look smart till they start to talk.
-
Martin# wrote:
Yes, a "make my homework, so I don't have to think!" message board is missing!
Not asking anyone to "make my homework", i simply couldn't get it working... Tried understanding it, tried to get it work and i couldn't, thats why i asked.
Light is faster than sound... Thats why some people look smart till they start to talk.
-
Martin# wrote:
Yes, a "make my homework, so I don't have to think!" message board is missing!
Not asking anyone to "make my homework", i simply couldn't get it working... Tried understanding it, tried to get it work and i couldn't, thats why i asked.
Light is faster than sound... Thats why some people look smart till they start to talk.
Tenkyu wrote:
Tried understanding it, tried to get it work and i couldn't, thats why i asked.
So what exactly don't you understand? The code snippet you were given is quite simple. It iterates over all list items by index, compares each item to the value of the textbox and then makes a call to the SetSelected method passing the the item index and a boolean indicating whether the item should be selected or not.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
Tenkyu wrote:
Tried understanding it, tried to get it work and i couldn't, thats why i asked.
So what exactly don't you understand? The code snippet you were given is quite simple. It iterates over all list items by index, compares each item to the value of the textbox and then makes a call to the SetSelected method passing the the item index and a boolean indicating whether the item should be selected or not.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
For the first i might add that i'm new to programming, it is one of my first projects i'm playin around with, so didn't know what "iterate" the list meant, and i didn't know how to use "SetSelected" method etc, i'm mostly trying my way there but error messages aint very funny. Anyways thanks for the help, got it all working now i think.
Light is faster than sound... Thats why some people look smart till they start to talk.