Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Listbox suggestion from textbox

Listbox suggestion from textbox

Scheduled Pinned Locked Moved C#
helpquestion
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D dudz artiaga

    Hi Everyone, Is there a way to display the suggestions in a list box with a key from textbox? let say I type in TextBox "ap" then listbox will show "apple" "appliances" "application" something like that? Thanks for your help.

    A Offline
    A Offline
    Abhinav S
    wrote on last edited by
    #2

    You will have to build your own logic to implement this kind of textbox. AutoCompleteMode[^] can be used - however, the dropdown still needs to be implemented. This sample[^] might help you.

    Apps - Color Analyzer | Arctic | XKCD | Sound Meter | Speed Dial

    D 1 Reply Last reply
    0
    • D dudz artiaga

      Hi Everyone, Is there a way to display the suggestions in a list box with a key from textbox? let say I type in TextBox "ap" then listbox will show "apple" "appliances" "application" something like that? Thanks for your help.

      S Offline
      S Offline
      SaqibRasheed
      wrote on last edited by
      #3

      Yes you can do this. The events of the TextBox will help you in this case. You need to capture key events, may be KeyDown, KeyUp, or KeyPress help you in that case. In that event, write code for fetching the list which you need to bind with the listbox on the basis of the text entered in the TextBox and then bind it. Hope it will work :)

      D 1 Reply Last reply
      0
      • D dudz artiaga

        Hi Everyone, Is there a way to display the suggestions in a list box with a key from textbox? let say I type in TextBox "ap" then listbox will show "apple" "appliances" "application" something like that? Thanks for your help.

        B Offline
        B Offline
        BillWoodruff
        wrote on last edited by
        #4

        You can achieve this functionality using only a WinForms ComboBox. Just set the ComboBox 'AutoComplete property to 'Suggest; and, set the 'AutoCompleteSoure property to 'ListItems. The AutoSuggest feature will work with the ComboBox 'DropDownStyle property set to either 'Simple, or 'DropDown, but not 'DropDownList. Enter a set of items, using the design-time editor, for example:

        aardvarks
        axolotls
        bears
        beavers
        bushmasters
        chickens
        civet cats
        coyotes
        dingos
        eagles
        foxes
        gorillas
        horses
        hyenas
        

        Run the application, and watch what's presented in the auto-dropdown when you type the character "b," and then observe what's presented when you enter "be." Note that you can set the font, color, style, etc. of what your type, what's displayed after your choice, in the text-entry area, but the suggest drop-down items will use a default Windows small-size font. You can also define a 'CustomSource for your 'Suggest list[^].

        ~ “This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal

        D 1 Reply Last reply
        0
        • A Abhinav S

          You will have to build your own logic to implement this kind of textbox. AutoCompleteMode[^] can be used - however, the dropdown still needs to be implemented. This sample[^] might help you.

          Apps - Color Analyzer | Arctic | XKCD | Sound Meter | Speed Dial

          D Offline
          D Offline
          dudz artiaga
          wrote on last edited by
          #5

          Thanks Abhinav, I tried that already but I think I should display the suggestion in Listbox to create an awesome effect. But thank you for the reply.

          1 Reply Last reply
          0
          • S SaqibRasheed

            Yes you can do this. The events of the TextBox will help you in this case. You need to capture key events, may be KeyDown, KeyUp, or KeyPress help you in that case. In that event, write code for fetching the list which you need to bind with the listbox on the basis of the text entered in the TextBox and then bind it. Hope it will work :)

            D Offline
            D Offline
            dudz artiaga
            wrote on last edited by
            #6

            Thanks SaqibRasheed, I think this was a great suggestion. I am just wondering if there's a quick solution for that, like you just have to set the property of a list box same with the autoComplete feature of textbox. Thanks.

            S 1 Reply Last reply
            0
            • B BillWoodruff

              You can achieve this functionality using only a WinForms ComboBox. Just set the ComboBox 'AutoComplete property to 'Suggest; and, set the 'AutoCompleteSoure property to 'ListItems. The AutoSuggest feature will work with the ComboBox 'DropDownStyle property set to either 'Simple, or 'DropDown, but not 'DropDownList. Enter a set of items, using the design-time editor, for example:

              aardvarks
              axolotls
              bears
              beavers
              bushmasters
              chickens
              civet cats
              coyotes
              dingos
              eagles
              foxes
              gorillas
              horses
              hyenas
              

              Run the application, and watch what's presented in the auto-dropdown when you type the character "b," and then observe what's presented when you enter "be." Note that you can set the font, color, style, etc. of what your type, what's displayed after your choice, in the text-entry area, but the suggest drop-down items will use a default Windows small-size font. You can also define a 'CustomSource for your 'Suggest list[^].

              ~ “This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal

              D Offline
              D Offline
              dudz artiaga
              wrote on last edited by
              #7

              Thanks BillWood I tried it before but I think I should display the suggestion on a listbox. Thanks anyway.

              B 1 Reply Last reply
              0
              • D dudz artiaga

                Thanks SaqibRasheed, I think this was a great suggestion. I am just wondering if there's a quick solution for that, like you just have to set the property of a list box same with the autoComplete feature of textbox. Thanks.

                S Offline
                S Offline
                SaqibRasheed
                wrote on last edited by
                #8

                I have worked with VS 2008 and there was no such property like autoComplete for TextBox. May be later versions of Visual Studio are having such property.

                B 1 Reply Last reply
                0
                • S SaqibRasheed

                  I have worked with VS 2008 and there was no such property like autoComplete for TextBox. May be later versions of Visual Studio are having such property.

                  B Offline
                  B Offline
                  BillWoodruff
                  wrote on last edited by
                  #9

                  It's probably the case you just never used TextBox's AutoComplete facility, and have forgotten it was there. AutoComplete facilities, like those in the ComboBox, have been implemented in the TextBox Control from .NET Framework 2.0~4.5, and in the .NET Client Profile from 3.5 SP1 to 4. Visual Studio 2008, November, 2007, used FrameWork 3.5.

                  ~ “This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal

                  1 Reply Last reply
                  0
                  • D dudz artiaga

                    Thanks BillWood I tried it before but I think I should display the suggestion on a listbox. Thanks anyway.

                    B Offline
                    B Offline
                    BillWoodruff
                    wrote on last edited by
                    #10

                    A ListBox does not implement any AutoComplete facilities; it's one of the simpler Controls. It has no "DropDown" behavior built into it. Of course, you could spend a few weeks building your own UserControl that contained a TextBox (which does support Auto-Complete), and somehow co-ordinate that with which items are displayed in a ListBox, and by the time you have wasted many hours developing that, and dealing with all the possible complications: you will have duplicated a ComboBox with AutoComplete, and that control is going to be very slow in response, compared to using a ComboBox :) good luck, Bill

                    ~ “This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal

                    1 Reply Last reply
                    0
                    • D dudz artiaga

                      Hi Everyone, Is there a way to display the suggestions in a list box with a key from textbox? let say I type in TextBox "ap" then listbox will show "apple" "appliances" "application" something like that? Thanks for your help.

                      T Offline
                      T Offline
                      TnTinMn
                      wrote on last edited by
                      #11

                      If I am understanding you goals correctly give this a try. Create a new usercontrol and place a textbox and listbox on it. Then add this as the code behind the control

                      using System;
                      using System.Collections.Generic;
                      using System.ComponentModel;
                      using System.Drawing;
                      using System.Data;
                      using System.Linq;
                      using System.Text;
                      using System.Windows.Forms;

                      namespace WindowsFormsApplication1
                      {
                      public partial class UserControl1 : UserControl
                      {
                      private DataTable autocompletelist;
                      public UserControl1()
                      {
                      InitializeComponent();

                              autocompletelist = new DataTable();
                              autocompletelist.Columns.Add("item", typeof(string));
                              autocompletelist.DefaultView.Sort = "\[item\] ASC";
                              listBox1.DataSource = autocompletelist;
                              listBox1.DisplayMember = "item";
                              listBox1.ValueMember = "item";
                              listBox1.Visible = false;
                              fillautocomplete();// load data in table
                              textBox1.TextChanged += textBox1\_TextChanged; // wire-up text change handler
                              listBox1.DoubleClick += listBox1\_DoubleClick; // wire-up double-click handler
                              this.Leave += new System.EventHandler(this.UserControl1\_Leave);
                          }
                          private void fillautocomplete()
                          {
                              // fill the datatable by any means that meets your needs, i.e. a database query
                              // for this example I will fill it manually
                              autocompletelist.Rows.Add(new object\[\] { "ape" });
                              autocompletelist.Rows.Add(new object\[\] { "apple" });
                              autocompletelist.Rows.Add(new object\[\] { "apppliance" });
                              autocompletelist.Rows.Add(new object\[\] { "application" });
                              autocompletelist.Rows.Add(new object\[\] { "boy" });
                              autocompletelist.Rows.Add(new object\[\] { "bat" });
                          }
                          private void textBox1\_TextChanged(object sender, System.EventArgs e)
                          {
                              // note the single quote usage in the filter
                              // see: http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx
                              autocompletelist.DefaultView.RowFilter = "\[item\] LIKE '" + textBox1.Text + "\*'";
                              listBox1.Visible = autocompletelist.DefaultView.Count > 0;
                          }
                      
                          private void listBox1\_DoubleClick(object sender, System.EventArgs e)
                          {
                      
                      B 1 Reply Last reply
                      0
                      • T TnTinMn

                        If I am understanding you goals correctly give this a try. Create a new usercontrol and place a textbox and listbox on it. Then add this as the code behind the control

                        using System;
                        using System.Collections.Generic;
                        using System.ComponentModel;
                        using System.Drawing;
                        using System.Data;
                        using System.Linq;
                        using System.Text;
                        using System.Windows.Forms;

                        namespace WindowsFormsApplication1
                        {
                        public partial class UserControl1 : UserControl
                        {
                        private DataTable autocompletelist;
                        public UserControl1()
                        {
                        InitializeComponent();

                                autocompletelist = new DataTable();
                                autocompletelist.Columns.Add("item", typeof(string));
                                autocompletelist.DefaultView.Sort = "\[item\] ASC";
                                listBox1.DataSource = autocompletelist;
                                listBox1.DisplayMember = "item";
                                listBox1.ValueMember = "item";
                                listBox1.Visible = false;
                                fillautocomplete();// load data in table
                                textBox1.TextChanged += textBox1\_TextChanged; // wire-up text change handler
                                listBox1.DoubleClick += listBox1\_DoubleClick; // wire-up double-click handler
                                this.Leave += new System.EventHandler(this.UserControl1\_Leave);
                            }
                            private void fillautocomplete()
                            {
                                // fill the datatable by any means that meets your needs, i.e. a database query
                                // for this example I will fill it manually
                                autocompletelist.Rows.Add(new object\[\] { "ape" });
                                autocompletelist.Rows.Add(new object\[\] { "apple" });
                                autocompletelist.Rows.Add(new object\[\] { "apppliance" });
                                autocompletelist.Rows.Add(new object\[\] { "application" });
                                autocompletelist.Rows.Add(new object\[\] { "boy" });
                                autocompletelist.Rows.Add(new object\[\] { "bat" });
                            }
                            private void textBox1\_TextChanged(object sender, System.EventArgs e)
                            {
                                // note the single quote usage in the filter
                                // see: http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx
                                autocompletelist.DefaultView.RowFilter = "\[item\] LIKE '" + textBox1.Text + "\*'";
                                listBox1.Visible = autocompletelist.DefaultView.Count > 0;
                            }
                        
                            private void listBox1\_DoubleClick(object sender, System.EventArgs e)
                            {
                        
                        B Offline
                        B Offline
                        BillWoodruff
                        wrote on last edited by
                        #12

                        :thumbsup:Interesting work TnTinMn ! I really enjoyed studying your code, and seeing what you did with a DataSet, and the AutoComplete filter, thanks. There are, however, some structural problems I see with your solution: 1. a ListBox has no 'AutoSize property: you never adjust its height, so, unless its height is equal to the possible maximum height (sum of the heights of all matches) of any possible single-character match, which, imho is undesirable visually. Then: 2. if the ListBox Height is too "short" to accommodate all the items of a given match: you will have to use the up-down selector at the ListBox right, to scroll to get to see all the possible matches. imho, that's an unacceptable burden on the user. 3. your solution will work for a single character entered at the start of a TextBox only. 4. when the TextBox is cleared, all the choices are displayed. 5. if you enter two spaces, followed by a valid letter, like "a" in this case, no choices appear. If you modify your code like this:

                        // original code by TnTinMn
                        // modified by bw ... Aug. 21, 2013
                        private void textBox1_TextChanged(object sender, System.EventArgs e)
                        {
                        // note that for this to work the UserControl must have 'AutoSize set to 'true !

                        // \* modified by bw ... Aug. 21, 2013
                        // quit if the TextBox is empty
                        // make sure the ListBox is not visible
                        if (string.IsNullOrWhiteSpace(textBox1.Text))
                        {
                            if (listBox1.Visible) listBox1.Visible = false;
                            return;
                        }
                        //\*
                        
                        // original code by TnTinMn
                        // note the single quote usage in the filter
                        // see: http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx
                        autocompletelist.DefaultView.RowFilter = "\[item\] LIKE '" + textBox1.Text + "\*'";
                        
                        // \* original code by TnTinMn
                        // commented out by bw ... Aug. 21, 2013
                        // listBox1.Visible = autocompletelist.DefaultView.Count > 0;
                        // \*
                        
                        // \* modified by bw ... Aug. 21, 2013
                        if (autocompletelist.DefaultView.Count > 0)
                        {
                            listBox1.Visible = true;
                        
                            // make sure the ListBox expands to shows all possible matches !
                            listBox1.Height = (autocompletelist.DefaultView.Count + 1) \* listBox1.ItemHeight;
                        }
                        else
                        {
                            listBox1.Visible = false;
                        }
                        //\*
                        

                        }

                        Now the user will see all the possible matches, filtered as you would expect, but the control's behavior still has the other problems mentioned above: with the code as it is now. While your solu

                        T 1 Reply Last reply
                        0
                        • B BillWoodruff

                          :thumbsup:Interesting work TnTinMn ! I really enjoyed studying your code, and seeing what you did with a DataSet, and the AutoComplete filter, thanks. There are, however, some structural problems I see with your solution: 1. a ListBox has no 'AutoSize property: you never adjust its height, so, unless its height is equal to the possible maximum height (sum of the heights of all matches) of any possible single-character match, which, imho is undesirable visually. Then: 2. if the ListBox Height is too "short" to accommodate all the items of a given match: you will have to use the up-down selector at the ListBox right, to scroll to get to see all the possible matches. imho, that's an unacceptable burden on the user. 3. your solution will work for a single character entered at the start of a TextBox only. 4. when the TextBox is cleared, all the choices are displayed. 5. if you enter two spaces, followed by a valid letter, like "a" in this case, no choices appear. If you modify your code like this:

                          // original code by TnTinMn
                          // modified by bw ... Aug. 21, 2013
                          private void textBox1_TextChanged(object sender, System.EventArgs e)
                          {
                          // note that for this to work the UserControl must have 'AutoSize set to 'true !

                          // \* modified by bw ... Aug. 21, 2013
                          // quit if the TextBox is empty
                          // make sure the ListBox is not visible
                          if (string.IsNullOrWhiteSpace(textBox1.Text))
                          {
                              if (listBox1.Visible) listBox1.Visible = false;
                              return;
                          }
                          //\*
                          
                          // original code by TnTinMn
                          // note the single quote usage in the filter
                          // see: http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx
                          autocompletelist.DefaultView.RowFilter = "\[item\] LIKE '" + textBox1.Text + "\*'";
                          
                          // \* original code by TnTinMn
                          // commented out by bw ... Aug. 21, 2013
                          // listBox1.Visible = autocompletelist.DefaultView.Count > 0;
                          // \*
                          
                          // \* modified by bw ... Aug. 21, 2013
                          if (autocompletelist.DefaultView.Count > 0)
                          {
                              listBox1.Visible = true;
                          
                              // make sure the ListBox expands to shows all possible matches !
                              listBox1.Height = (autocompletelist.DefaultView.Count + 1) \* listBox1.ItemHeight;
                          }
                          else
                          {
                              listBox1.Visible = false;
                          }
                          //\*
                          

                          }

                          Now the user will see all the possible matches, filtered as you would expect, but the control's behavior still has the other problems mentioned above: with the code as it is now. While your solu

                          T Offline
                          T Offline
                          TnTinMn
                          wrote on last edited by
                          #13

                          Bill, I agree that this essentially duplicates that the function of the ComboBox and I see no need for it myself. I had read through this thread and seen that you were trying to steer the OP to using a ComboBox (I agree that this makes the most sense), but he/she seemed insistent on having a separate list box. My primary goal was to show a simple way to create the filtered list and get it presented in the ListBox. I was not focused too much on the control's usability or aesthetics as those criteria would have been my own and not the OP's; after all, this is not an article on how to write a replacement ComboBox. :)

                          Quote:

                          3. your solution will work for a single character entered at the start of a TextBox only.

                          I don't understand this statement. The filter will find all matches that start with the TextBox text.

                          Quote:

                          5. if you enter two spaces, followed by a valid letter, like "a" in this case, no choices appear

                          This by design (sheesh, I sound like MS justifying a bug :laugh:). It is the same filterin behavior that the ComboBox autocomplete has. I'm glad that you had some fun playing with it.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups