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:
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.