programmatically set the SelectedItems for a ListBox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tommcd24

    programmatically set the SelectedItems for a ListBox

    Hello all,

    I need a way to read a previously saved list of customers and
    programatically "Select" these items in an existing listbox. Something
    to the effect of ListBox.Selecte dItems = myCollection. Of course
    SelectedItems is readonly.

    I'm reading in from an XML file and I have that portion of the issue
    resolved. Where I run into problems is how to get them all selected.

    Any suggestions?

  • Armin Zingler

    #2
    Re: programmaticall y set the SelectedItems for a ListBox

    "tommcd24" <tommcd24@gmail .comschrieb
    Hello all,
    >
    I need a way to read a previously saved list of customers and
    programatically "Select" these items in an existing listbox.
    Something to the effect of ListBox.Selecte dItems = myCollection. Of
    course SelectedItems is readonly.
    >
    I'm reading in from an XML file and I have that portion of the issue
    resolved. Where I run into problems is how to get them all selected.
    >
    Any suggestions?

    lbx.SetSelected (.., True)



    Armin

    Comment

    • tommcd24

      #3
      Re: programmaticall y set the SelectedItems for a ListBox

      Thanks Armin,

      SetSelected wants an index and I'm difficulty translating the values
      from my xml file which correspond to the ValueMember property of the
      listbox items. I've looked into ListBox.FindStr ing() and
      ListBox.IndexOf () but both return -1. I'm guessing they must be
      searching the DisplayMember property.

      Any thoughts on how to translate from the ValueMember values to Index?


      Comment

      • tommcd24

        #4
        Re: programmaticall y set the SelectedItems for a ListBox

        Does anyone have suggestions or sample code to do this? I need to be
        able to Select listbox items based on the item value.

        Thanks in advance,

        Comment

        • Mr. Arnold

          #5
          Re: programmaticall y set the SelectedItems for a ListBox


          "tommcd24" <tommcd24@gmail .comwrote in message
          news:1183491777 .440313.155340@ k29g2000hsd.goo glegroups.com.. .
          Does anyone have suggestions or sample code to do this? I need to be
          able to Select listbox items based on the item value.
          >
          I don't know if you're looking for a routine below or not to set the combox
          to the display is display value based based on its item value or not, but it
          might help you, possibly.


          Sub SelectCBOBbyIte m(ctrl as Combobox, someval as whatevertype)

          dim i as Integer
          For i = 0 To ComboBox1.Items .Count - 1

          ComboBox1.Selec tedIndex = i

          If ComboBox1.Selec tedValue = someval Then exit sub

          Next

          end sub

          Comment

          • Mr. Arnold

            #6
            Re: programmaticall y set the SelectedItems for a ListBox


            "Mr. Arnold" <MR. Arnold@Arnold.c omwrote in message
            news:uwVtfkbvHH A.4916@TK2MSFTN GP04.phx.gbl...
            >
            "tommcd24" <tommcd24@gmail .comwrote in message
            news:1183491777 .440313.155340@ k29g2000hsd.goo glegroups.com.. .
            >Does anyone have suggestions or sample code to do this? I need to be
            >able to Select listbox items based on the item value.
            >>
            >
            >
            Oops, you would be using the ctrl

            call SelectCBOBbyIte m(Combox1, value)

            Sub SelectCBOBbyIte m(ctrl as Combobox, someval as whatevertype)

            dim i as Integer
            For i = 0 To ctrl.Items.Coun t - 1

            ctrl.SelectedIn dex = i

            If ctrl.SelectedVa lue = someval Then exit sub

            Next

            end sub


            >

            Comment

            • tommcd24

              #7
              Re: programmaticall y set the SelectedItems for a ListBox

              I was overcomplicatin g this issue. I was assuming that
              ListBox.Selecte dValue could only hold 1 value. On a lark I tried
              repeated calls using that property and achieved the result I needed of
              selecting multiple items based on the ValueMember. I'm including my
              code below for others who may have similar situations.

              Using xReader As XmlReader = XmlReader.Creat e(strFilePath & "\"
              & strFileName)
              xReader.ReadSta rtElement("Cust omers")
              While xReader.Read()
              If (xReader.IsStar tElement()) Then
              If (xReader.IsEmpt yElement()) Then
              Me.lstCustomers .SelectedValue = xReader(0)
              End If
              End If
              End While
              End Using

              Comment

              Working...