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. Multiple select in listbox

Multiple select in listbox

Scheduled Pinned Locked Moved C#
helpquestion
12 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.
  • A Andrew Em

    Hi I'm building a little "search" function for my listbox! This is what i want to happend: I got a textbox, then i write the number (the listbox contains a list of numbers) i want to find. The number i write in the textbox should then get selected in the listbox when i click a button. That far, it works great with this code: lstlistBox1.SelectedItem = txttextBox1.Text; Now to the problem. If the number i write in the textbox exists more than one time in the list, i want all the items with that name to get selected. Been trying with loops and stuff to make it happend but just wont work. Anyone got a suggestion? Thx in advance

    Light is faster than sound... Thats why some people look smart till they start to talk.

    S Offline
    S Offline
    Stefan Troschuetz
    wrote on last edited by
    #2

    Iterate over the listbox items and use the SetSelect method to select or deselect the items according to the number in the textbox.


    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

    www.troschuetz.de

    A 1 Reply Last reply
    0
    • S Stefan Troschuetz

      Iterate over the listbox items and use the SetSelect method to select or deselect the items according to the number in the textbox.


      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

      www.troschuetz.de

      A Offline
      A Offline
      Andrew Em
      wrote on last edited by
      #3

      think you could explain how i do that or add a code sample? Would appreciate it. Thanks

      Light is faster than sound... Thats why some people look smart till they start to talk.

      1 Reply Last reply
      0
      • A Andrew Em

        Hi I'm building a little "search" function for my listbox! This is what i want to happend: I got a textbox, then i write the number (the listbox contains a list of numbers) i want to find. The number i write in the textbox should then get selected in the listbox when i click a button. That far, it works great with this code: lstlistBox1.SelectedItem = txttextBox1.Text; Now to the problem. If the number i write in the textbox exists more than one time in the list, i want all the items with that name to get selected. Been trying with loops and stuff to make it happend but just wont work. Anyone got a suggestion? Thx in advance

        Light is faster than sound... Thats why some people look smart till they start to talk.

        V Offline
        V Offline
        VijayaRam
        wrote on last edited by
        #4

        Hai do the following: //Add a button and add the following code to that { listBox1.SelectionMode = SelectionMode.MultiExtended; string strSearch = textBox1.Text; for (int i = 0,j=0; i < listBox1.Items.Count; i++) { if (listBox1.Items[i].ToString() == strSearch) listBox1.SetSelected(i, true); else listBox1.SetSelected(i, false); } } Add a textbox and a listbox and try to execute. regards,

        VijayaRam

        A 1 Reply Last reply
        0
        • V VijayaRam

          Hai do the following: //Add a button and add the following code to that { listBox1.SelectionMode = SelectionMode.MultiExtended; string strSearch = textBox1.Text; for (int i = 0,j=0; i < listBox1.Items.Count; i++) { if (listBox1.Items[i].ToString() == strSearch) listBox1.SetSelected(i, true); else listBox1.SetSelected(i, false); } } Add a textbox and a listbox and try to execute. regards,

          VijayaRam

          A Offline
          A Offline
          Andrew Em
          wrote on last edited by
          #5

          Good one, worked on first try :-D Thanks! Next part of the application is to make another listbox select the same indexes as the first one does, heres also the problem that it only selects one. (note: the same indexes, not the same numbers :)) Any suggestions?

          Light is faster than sound... Thats why some people look smart till they start to talk.

          S 1 Reply Last reply
          0
          • A Andrew Em

            Good one, worked on first try :-D Thanks! Next part of the application is to make another listbox select the same indexes as the first one does, heres also the problem that it only selects one. (note: the same indexes, not the same numbers :)) Any suggestions?

            Light is faster than sound... Thats why some people look smart till they start to talk.

            S Offline
            S Offline
            Stefan Troschuetz
            wrote on last edited by
            #6

            Did you even try to understand why and how the code snippet you were given solved your original problem? I doubt that cause otherwise, you would know how to easily solve this one.


            "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

            www.troschuetz.de

            M 1 Reply Last reply
            0
            • S Stefan Troschuetz

              Did you even try to understand why and how the code snippet you were given solved your original problem? I doubt that cause otherwise, you would know how to easily solve this one.


              "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

              www.troschuetz.de

              M Offline
              M Offline
              Martin 0
              wrote on last edited by
              #7

              :mad: Yes, a "make my homework, so I don't have to think!" message board is missing!

              A D 2 Replies Last reply
              0
              • M Martin 0

                :mad: Yes, a "make my homework, so I don't have to think!" message board is missing!

                A Offline
                A Offline
                Andrew Em
                wrote on last edited by
                #8

                Martin# wrote:

                Yes, a "make my homework, so I don't have to think!" message board is missing!

                Not asking anyone to "make my homework", i simply couldn't get it working... Tried understanding it, tried to get it work and i couldn't, thats why i asked.

                Light is faster than sound... Thats why some people look smart till they start to talk.

                M S 2 Replies Last reply
                0
                • M Martin 0

                  :mad: Yes, a "make my homework, so I don't have to think!" message board is missing!

                  D Offline
                  D Offline
                  Dan Neely
                  wrote on last edited by
                  #9

                  Will this[^] do?

                  -- Rules of thumb should not be taken for the whole hand.

                  1 Reply Last reply
                  0
                  • A Andrew Em

                    Martin# wrote:

                    Yes, a "make my homework, so I don't have to think!" message board is missing!

                    Not asking anyone to "make my homework", i simply couldn't get it working... Tried understanding it, tried to get it work and i couldn't, thats why i asked.

                    Light is faster than sound... Thats why some people look smart till they start to talk.

                    M Offline
                    M Offline
                    Martin 0
                    wrote on last edited by
                    #10

                    Don't take it too personal ;)

                    Tenkyu wrote:

                    Light is faster than sound... Thats why some people look smart till they start to talk.

                    Like that one!

                    1 Reply Last reply
                    0
                    • A Andrew Em

                      Martin# wrote:

                      Yes, a "make my homework, so I don't have to think!" message board is missing!

                      Not asking anyone to "make my homework", i simply couldn't get it working... Tried understanding it, tried to get it work and i couldn't, thats why i asked.

                      Light is faster than sound... Thats why some people look smart till they start to talk.

                      S Offline
                      S Offline
                      Stefan Troschuetz
                      wrote on last edited by
                      #11

                      Tenkyu wrote:

                      Tried understanding it, tried to get it work and i couldn't, thats why i asked.

                      So what exactly don't you understand? The code snippet you were given is quite simple. It iterates over all list items by index, compares each item to the value of the textbox and then makes a call to the SetSelected method passing the the item index and a boolean indicating whether the item should be selected or not.


                      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                      www.troschuetz.de

                      A 1 Reply Last reply
                      0
                      • S Stefan Troschuetz

                        Tenkyu wrote:

                        Tried understanding it, tried to get it work and i couldn't, thats why i asked.

                        So what exactly don't you understand? The code snippet you were given is quite simple. It iterates over all list items by index, compares each item to the value of the textbox and then makes a call to the SetSelected method passing the the item index and a boolean indicating whether the item should be selected or not.


                        "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                        www.troschuetz.de

                        A Offline
                        A Offline
                        Andrew Em
                        wrote on last edited by
                        #12

                        For the first i might add that i'm new to programming, it is one of my first projects i'm playin around with, so didn't know what "iterate" the list meant, and i didn't know how to use "SetSelected" method etc, i'm mostly trying my way there but error messages aint very funny. Anyways thanks for the help, got it all working now i think.

                        Light is faster than sound... Thats why some people look smart till they start to talk.

                        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