Click here to Skip to main content
15,887,471 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
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:

C#
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"]; //name is coming from database
                   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);
                }


            }


        }
Posted
Updated 8-Sep-16 12:17pm
v2
Comments
[no name] 8-Sep-16 12:35pm    
if "Is there anyway to do this?" is your actual question, then your answer is "Yes, there are many ways you can do this."
ZurdoDev 8-Sep-16 13:49pm    
No, they did not ask "are there many ways to do this." ;) Yes, there is anyway to do this.
[no name] 8-Sep-16 14:21pm    
Thank you. I guess my attempt to use psychic powers, like so many others seem to have, has failed miserably.
Maciej Los 8-Sep-16 14:48pm    
A question is tagged as ASP.NET but the code and the title of qestion correspond to WinForms.
[no name] 8-Sep-16 14:52pm    
Yeah and try and figure out how "Cat, Dot, Carrot,and Barcolli" are medicines.... Barcolli might be a medicine though. I have no idea what a barcolli might be.

Start here: Search As You Type in C#[^]. This might help you in finding a way to implement search future in your WinForm.
Try!
 
Share this answer
 
Assuming you want to achieve this in winform.

here is an auto suggest textbox, please have a look, hope this will help you

autosuggest textbox
[^]
 
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