List box selection
-
If I have items in a list box like: Apple Banana Carrot How can I program the box to go to the selected item if I start typing into the drop down? For example, if I type A, I want Apple to appear then. Thanks!
You can use foreach to step over the items in the listbox, then if you find one that matches ( using whatever rules you want ) then you can make an item selected. Something like string searchString = tbSearch.Text.ToLower(); foreach(ListBoxItem item in lbItems.Items) { if (item.Text.Length >= searchString.Length) { if (item.Text.SubString(0, searchString.Length).ToLower() == searchString)) { item.Selected = true; break; } } } This code does not unselect the item selected before, but if the box is set to single select, that won't be an issue.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
You can use foreach to step over the items in the listbox, then if you find one that matches ( using whatever rules you want ) then you can make an item selected. Something like string searchString = tbSearch.Text.ToLower(); foreach(ListBoxItem item in lbItems.Items) { if (item.Text.Length >= searchString.Length) { if (item.Text.SubString(0, searchString.Length).ToLower() == searchString)) { item.Selected = true; break; } } } This code does not unselect the item selected before, but if the box is set to single select, that won't be an issue.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
You can use foreach to step over the items in the listbox, then if you find one that matches ( using whatever rules you want ) then you can make an item selected. Something like string searchString = tbSearch.Text.ToLower(); foreach(ListBoxItem item in lbItems.Items) { if (item.Text.Length >= searchString.Length) { if (item.Text.SubString(0, searchString.Length).ToLower() == searchString)) { item.Selected = true; break; } } } This code does not unselect the item selected before, but if the box is set to single select, that won't be an issue.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Launching a new VB dialect? Happens to me too!
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
-
Launching a new VB dialect? Happens to me too!
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
LOL - I wrote that without testing it, I suspect the semicolon will be the least of the issues with it. Hopefully it was close enough to push him in the right direction.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
If I have items in a list box like: Apple Banana Carrot How can I program the box to go to the selected item if I start typing into the drop down? For example, if I type A, I want Apple to appear then. Thanks!
Actually the easiest way to achive this is much simpler: put this in the dropdown_textchanged event:
Me.ListBox1.SelectedIndex = ListBox1.FindString(Me.ComboBox1.Text)
Please check out my articles: The ANZAC's articles
-
If I have items in a list box like: Apple Banana Carrot How can I program the box to go to the selected item if I start typing into the drop down? For example, if I type A, I want Apple to appear then. Thanks!
if statement using
A.Satheesh
-
Actually the easiest way to achive this is much simpler: put this in the dropdown_textchanged event:
Me.ListBox1.SelectedIndex = ListBox1.FindString(Me.ComboBox1.Text)
Please check out my articles: The ANZAC's articles
-
LOL - I wrote that without testing it, I suspect the semicolon will be the least of the issues with it. Hopefully it was close enough to push him in the right direction.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )