drop down list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prokopis
    New Member
    • Nov 2007
    • 30

    drop down list

    am new in c# and i have some problems that o need some help
    i have t text box that when i type the characters i want a drop down list to be appeard under the text box sorted according the characters that i type.
    if i type ont he textbox "myn" the dropdown list to be appeaerd sorted and according the first characters that i write.also this values that are going to be display must be retrieve from a database from sql server.
    i try doing something but i find dificulties.

    Code:
    class MyListBoxItem 
        {
    
        public MyListBoxItem(int ItemData, string Text)
            {
                text = Text;
                itemData = ItemData;
            }   
        public int ItemData
            {
            get
                {
                return itemData;
                }
            set
                {
                itemData = value;
                }
            }
         public override string ToString()
             {
             return text.Trim();
             }
         protected string text;
         protected int itemData;
        }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                
                if (capturesymptom.Text.Length > 1)
                {
    
                    SearchName();
    
                }
    
     }
    
        private void SearchName()
        {
            ListBox ListBox1 = new ListBox();
            ListBox1.Items.Clear();
            SqlConnection dataConnectionSymptom = null;
            DataSet datasetsymptom = null;
            
    //connection with the database//
    
            SqlDataAdapter dataAdaptersymptom = new SqlDataAdapter(querysymptom);
            dataAdaptersymptom.Fill(datasetsymptom, "[Symptoms]");
    
            
            datasetsymptom.DataSetName = "[DiseaseName]";
            dataConnectionSymptom.Close();
    
            for (int i = 0; i <  datasetsymptom.Tables["[Symptoms]"].Rows.Count; i++)
                {
                    ListBox1.Items.Add(new MyListBoxItem(Convert.ToInt32(datasetsymptom.Tables["Symptoms"].Rows[i][0].ToString()),datasetsymptom.Tables["Symptoms"].Rows[i][1].ToString()));
                }
                try
                {
                    
      //i thing the problem is in this step.i don't know how i should diplay the data that i get from the database under my text box
                }
                catch
                {
                    MessageBox.Show("search failed");
                }
                finally
                {
                    dataConnectionSymptom.Close();
                }
    
    }
    i thing that the problem is in the try statement.i dont know hot to diplay the data.
    beside that i don't know if the mylistboxitem is correct.
    the values that are going to be display are vchar.
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Have you tried using a combobox?

    Comment

    • prokopis
      New Member
      • Nov 2007
      • 30

      #3
      no i did't try a compobox.i don't want a combobox.this that i want is to have a text box.the user will write a word and from the textbox will load all the data according the database a listbox will appeard.
      ex fromt he database

      ID Symptom
      1 Qname1
      2 Qname2
      3 Pname1
      4 Pname2
      5 Zname
      if the user type Qna then on the listbox the Qname1 and Qname2 must be appeared.

      Comment

      • kenobewan
        Recognized Expert Specialist
        • Dec 2006
        • 4871

        #4
        Suggest you try a combobox.

        Comment

        • prokopis
          New Member
          • Nov 2007
          • 30

          #5
          Originally posted by kenobewan
          Suggest you try a combobox.
          my problem is how to load the data from the database.
          am not using combobox because on the database there are 1000 record.i don't want all those records to be loaded each time.i want only to be loaded sorted the user type on the textbox

          Comment

          • nateraaaa
            Recognized Expert Contributor
            • May 2007
            • 664

            #6
            Originally posted by prokopis
            my problem is how to load the data from the database.
            am not using combobox because on the database there are 1000 record.i don't want all those records to be loaded each time.i want only to be loaded sorted the user type on the textbox
            You could still use a combobox. You would just have to modify your stored procedure to only return records from the database that start with or contain (whatever you are trying to show the user) the the text that the user enters in the textbox. Populate your combobox with the records returned from the stored procedure. If you do this your calls to your database and populating your combobox will occur in the TextChanged event of your textbox.

            Nathan

            Comment

            • prokopis
              New Member
              • Nov 2007
              • 30

              #7
              Originally posted by nateraaaa
              You could still use a combobox. You would just have to modify your stored procedure to only return records from the database that start with or contain (whatever you are trying to show the user) the the text that the user enters in the textbox. Populate your combobox with the records returned from the stored procedure. If you do this your calls to your database and populating your combobox will occur in the TextChanged event of your textbox.

              Nathan
              ok.your idea is very good but there is a small problem.how am i going to do this.i just start using c# and i don't know a lot of things.if you could help me how to do this i will appreciate.

              Comment

              • kenobewan
                Recognized Expert Specialist
                • Dec 2006
                • 4871

                #8
                prokopis has already told you how to do it, if you don't know where to start then you need to complete the course that you are doing to learn the basics. Then show us your code if you have a problem or error. Thanks.

                Comment

                • prokopis
                  New Member
                  • Nov 2007
                  • 30

                  #9
                  i have try it but i have a question.
                  before i write something on the box and just oush the arrow to show all the list is not sorted.how can i sort it?

                  Comment

                  • terminul
                    New Member
                    • Nov 2007
                    • 6

                    #10
                    Maybe you can try the Ajax Control Toolkit's AutoCompleteExt ender.

                    it is simple to implement (via web services or Page /WebMethods)

                    Build web apps and services that run on Windows, Linux, and macOS using C#, HTML, CSS, and JavaScript. Get started for free on Windows, Linux, or macOS.


                    just download the toolkit zip and install the dlls to get these ajax controls, autocompleteext ender is just one of many.

                    also supports caching

                    Comment

                    Working...