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 selecteditems

Listbox selecteditems

Scheduled Pinned Locked Moved C#
help
3 Posts 2 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 Offline
    D Offline
    Drew McGhie
    wrote on last edited by
    #1

    I have a listbox(default except theat the selectionmode is "multisimple") that I'm populating dynamically through a BindingSource, with the display and value member set to the same column. On loading of the form, I need to set the initial selected value(s) to a previously-stored value. I declare a List called toSelect and fill it with the correct values. My problem is actually taking those values and selecting the correct listbox item(s). What I currently have is:

    foreach (string s in toSelect)
    {
       if (uxlbSuite.Items.Contains(s))
       {
          uxlbSuite.SelectedItems.Add(s);
       }
    }
    

    The problem is that uxlbSuite.Items.Contains(s) always returns false, and the uxlbSuite.SelectedItems.Add(s) doesn't actually do anything if I don't perform the if check. I think I'm accessing the SelectedItems list wrong, but I'm baffled as to the correct way, and advice would be greatly appreciated.

    L 1 Reply Last reply
    0
    • D Drew McGhie

      I have a listbox(default except theat the selectionmode is "multisimple") that I'm populating dynamically through a BindingSource, with the display and value member set to the same column. On loading of the form, I need to set the initial selected value(s) to a previously-stored value. I declare a List called toSelect and fill it with the correct values. My problem is actually taking those values and selecting the correct listbox item(s). What I currently have is:

      foreach (string s in toSelect)
      {
         if (uxlbSuite.Items.Contains(s))
         {
            uxlbSuite.SelectedItems.Add(s);
         }
      }
      

      The problem is that uxlbSuite.Items.Contains(s) always returns false, and the uxlbSuite.SelectedItems.Add(s) doesn't actually do anything if I don't perform the if check. I think I'm accessing the SelectedItems list wrong, but I'm baffled as to the correct way, and advice would be greatly appreciated.

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

      Hi Drew, to locate an item by its string value you want FindString() or FindStringExact() The SelectedItems collection represents an observed state of the listbox, changing it does not change the listbox state. You want SetSelected(). :)

      Luc Pattyn

      D 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi Drew, to locate an item by its string value you want FindString() or FindStringExact() The SelectedItems collection represents an observed state of the listbox, changing it does not change the listbox state. You want SetSelected(). :)

        Luc Pattyn

        D Offline
        D Offline
        Drew McGhie
        wrote on last edited by
        #3

        Thanks for the response. What I ended up getting working was looping through the items list (which will never be more than a dozen or so), rather than the List<> of items that needed to select. From that, I could parse the correct object from the associated datarow (by casting it as such) and then compare that to the list of approved items.

        uxlbSuite.SelectedItems.Clear();
        List objectsToAdd = new List();
        foreach (object unit in uxlbSuite.Items)
        {
           System.Data.DataRowView Row = unit as System.Data.DataRowView;
           if (Row != null && toSelect.Contains(Row["ID"].ToString()))
           {
              objectsToAdd.Add(unit);
           }
        }
        
        foreach (object unit in objectsToAdd)
        {
           uxlbSuite.SelectedItems.Add(unit);
        }
        
        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