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 Item Selection

Listbox Item Selection

Scheduled Pinned Locked Moved C#
question
5 Posts 3 Posters 5 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.
  • B Offline
    B Offline
    Brendan Vogt
    wrote on last edited by
    #1

    Hi, How does one check if an item in a listbox is selected? Usually I would do it like this if I have multiple items selected: foreach (ListItem item in lbFacilities.Items) { if (item.Selected) ...code here... } Is the best way like this (even if the SelectionMode property is not set to Multiple? Or would the following also be appropriate? if (lbWineries.SelectedValue == string.Empty) ...code here... Regards, ma se

    M A 2 Replies Last reply
    0
    • B Brendan Vogt

      Hi, How does one check if an item in a listbox is selected? Usually I would do it like this if I have multiple items selected: foreach (ListItem item in lbFacilities.Items) { if (item.Selected) ...code here... } Is the best way like this (even if the SelectionMode property is not set to Multiple? Or would the following also be appropriate? if (lbWineries.SelectedValue == string.Empty) ...code here... Regards, ma se

      M Offline
      M Offline
      malikjhangirahmed hotmail com
      wrote on last edited by
      #2

      I think the best way to check for selected items is to use SelectedIndex if the selection mode property is One or SelectedIndices if its multiple. Regards, Wasif Ehsan.

      B 1 Reply Last reply
      0
      • M malikjhangirahmed hotmail com

        I think the best way to check for selected items is to use SelectedIndex if the selection mode property is One or SelectedIndices if its multiple. Regards, Wasif Ehsan.

        B Offline
        B Offline
        Brendan Vogt
        wrote on last edited by
        #3

        Thanks for the reply. You mean like (for single selection): if (lbWineries.SelectedIndex == 0) Is this right?

        1 Reply Last reply
        0
        • B Brendan Vogt

          Hi, How does one check if an item in a listbox is selected? Usually I would do it like this if I have multiple items selected: foreach (ListItem item in lbFacilities.Items) { if (item.Selected) ...code here... } Is the best way like this (even if the SelectionMode property is not set to Multiple? Or would the following also be appropriate? if (lbWineries.SelectedValue == string.Empty) ...code here... Regards, ma se

          A Offline
          A Offline
          albCode
          wrote on last edited by
          #4

          for (int i =0;i<=lbFacilities.Items.Count -1;i++) { if (lbFacilities.Items[i].Selected ==true) ...code here... }

          B 1 Reply Last reply
          0
          • A albCode

            for (int i =0;i<=lbFacilities.Items.Count -1;i++) { if (lbFacilities.Items[i].Selected ==true) ...code here... }

            B Offline
            B Offline
            Brendan Vogt
            wrote on last edited by
            #5

            Is this the correct way to check if a single item is selected? Even if the selectionmode is not set to multiple?

            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