Click here to Skip to main content
15,886,626 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
does any one can help me with searching in c# windows form.

e.x
I have a listbox with name:

Mike
Antonio
Mark
Jackie
Bruss
Alonso
...
...
...

so If I insert a text box and then when a just write the first character then all names that start with that character to show...
Posted
Comments
Sandeep Mewara 26-Apr-12 10:41am    
Here is what is expected of enquirers:
1. TRY first what you want to do!
2. Formulate what was done by you that looks like an issue/not working.

Try them and tell if you face issues.
Members will be more than happy to help like this.

In the question it is mentioned that when first character is written then all names starting with that character shall be shown in the TextBox. Since, TextBox can not show multiple names, ComboBox can be used for this purpose, by appropriately setting the DataSource, DropDownStyle, AutoCompleteSource and AutoCompleteMode properties as shown below:
C#
ListBox listBox1 = new ListBox();
List<string> names = new List<string>() {
"Mike", "Antonio","Mark",
"Jackie", "Bruss","Alonso"
};
BindingSource bindingSource1 = new BindingSource();
bindingSource1.DataSource = names;

ComboBox comboBox1 = new ComboBox();
comboBox1.Dock = DockStyle.Bottom;
comboBox1.DataSource = bindingSource1;
comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

listBox1.Dock = DockStyle.Fill;
listBox1.DataSource = bindingSource1;

Controls.Add(comboBox1);
Controls.Add(listBox1);

To run the above sample, create a Windows Forms project in Visual Studio, double click on Form1 in the designer, which opens the code file, paste the above code in the Load event of Form1 and run the application.
 
Share this answer
 
v3
then use this

C#
private string[] ValArray;
private void Form1_Load(object sender, System.EventArgs e)
{
        // set value for ValArray

	ListBox1.Items.AddRange(ValArray);
}
private void TextBox1_TextChanged(System.Object sender, System.EventArgs e)
{
        if (TextBox1.Text == string.Empty){
                ListBox1.Items.AddRange(ValArray);
                return;
        }

	ListBox1.Items.Clear();
	foreach (string item in ValArray) {
		if (item.StartsWith(TextBox1.Text)) {
			ListBox1.Items.Add(item);
		}

	}
}
 
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