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. Visual Basic
  4. List box selection

List box selection

Scheduled Pinned Locked Moved Visual Basic
questiontutorial
9 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.
  • S Offline
    S Offline
    sa_runner
    wrote on last edited by
    #1

    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!

    C T S 3 Replies Last reply
    0
    • S sa_runner

      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!

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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 )

      S L 2 Replies Last reply
      0
      • C Christian Graus

        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 )

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

        Thank you!

        1 Reply Last reply
        0
        • C Christian Graus

          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 )

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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


          C 1 Reply Last reply
          0
          • L Luc Pattyn

            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


            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            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 )

            S 1 Reply Last reply
            0
            • S sa_runner

              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!

              T Offline
              T Offline
              The ANZAC
              wrote on last edited by
              #6

              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

              S 1 Reply Last reply
              0
              • S sa_runner

                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!

                S Offline
                S Offline
                satheesh a
                wrote on last edited by
                #7

                if statement using

                A.Satheesh

                1 Reply Last reply
                0
                • T The ANZAC

                  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

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

                  Thanks. I'll try that. I knew it had to be something I wasn't doing just right.

                  1 Reply Last reply
                  0
                  • C Christian Graus

                    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 )

                    S Offline
                    S Offline
                    sa_runner
                    wrote on last edited by
                    #9

                    I was a little freaked by the code, but got the general idea of what you were trying to say. Again I appreciate all the help. This is a great site to get ideas for things and how to do things.

                    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