3
down vote
favorite
2
I have a large amount of items in a listBox called listBox1. I also have a textBox (textBox1) at the top. I want to be able to type into the textBox and the listBox searches through its items and finds ones that contain what I am typing.
For example, say the listBox contains
Cat
Dot
Carrot
and Barcolli
If I start typing the letter
C
, then I want it to show both
Cat
and
Carrot
, when i type a it should keep showing them both, but when I add an
r
it should remove
Cat
from the list. Is there anyway to do this?
i am fetching listbox from databse and then when we type text in textbox according to that text item show in textbox
What I have tried:
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(Edit.connectionname());
string query = "select MEDICINE from tinchure";
SqlCommand cmd = new SqlCommand(query, con);
DataTable data = new DataTable();
SqlDataReader dbr;
try
{
con.Open();
dbr = cmd.ExecuteReader();
while (dbr.Read())
{
string sname = (string)dbr["MEDICINE"];
listBox1.Items.Add(sname);
string[] array = new string[listBox1.Items.Count];
for (int i = 0; i < listBox1.Items.Count; i++)
{
object s = listBox1.Items[i];
array[i] = s.ToString();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
listBox1.Items.Clear();
foreach (string str in )
{
if (str.StartsWith(textBox5.Text, StringComparison.CurrentCultureIgnoreCase))
{
listBox1.Items.Add(str);
}
}
}