Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Suppose I have two listbox.And one
searchTextbox
On listbox1 contains -
Apple
Alva
Alpha
Angles
Any
Able
Bubble
Ball
Bislesion
Biscuit
Cat
Cool
etc etc.
on searchTextbox I type - A
then show all words on listbox2 which are
started with A.
on Listbox2 will show -
Apple
alva
Alpha
...
...
...
Thanks in advanced
Posted
Comments
borntrouble 27-Oct-13 11:53am    
Please let us know if you need it for a Windows Based or ASP.NET page.
[no name] 27-Oct-13 14:30pm    
borntrouble firstly thanks for your response.
I need it in -
winform (vb.net or csharp)

1 solution

In the searchTextbox subscribe to the TextChanged event. Within that listener use

int searchLength = searchTextbox.Text.Length;

listbox2.Items.Clear();
for (int i=0; i<listbox1.items.count;i++)   
if( listbox1.Items[i].ToString().substring(0,searchLength) == searchTextbox.Text)
      listbox2.Items.Add(listbox1.Items[i].ToString());


Please note my first submission I accidentally left out the ToString() within the if-statement. The above code is now correct, It had previously read

if( listbox1.Items[i].substring(0,searchLength) == searchTextbox.Text)
 
Share this answer
 
v3
Comments
[no name] 28-Oct-13 0:43am    
Firstly thanks for your nice reply.

if( listbox1.Items[i].substring

DEBUG

1 object'does not contain a definition fosubstring'and no extension method
substring'accepting a first argument of typeobject'could be found (are you
missing a using directive or an assembly referen?)
MrGlass3 28-Oct-13 1:03am    
I'm sorry you would have to use the ToString() method before substring.

That if-statement should be:

if( listbox1.Items[i].ToString().substring(0,searchLength) == searchTextbox.Text)

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