Iterating through a ListBox in VB.NET VS 2005

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

    Iterating through a ListBox in VB.NET VS 2005

    I have a listbox on a Windows form and it is bound using the following code:

    objListBox.Data Source = tbl

    objListBox.Disp layMember = "ContactNam e"

    objListBox.Valu eMember = "ContactID"

    ------------------------------------

    On my form, I have a multi-select listbox and I would like to select a few
    items. So If my listbox contains: CA, NV, OR, WA, AZ

    and I choose CA and NV,



    How can I get get those items?

    I tried something like

    dim itm as object

    for each itm in objlistbox.sele cteditems

    console.writeli ne(itm)

    next



    but it doesn't work. Why is this so hard to get the selected items in a
    listbox?



    Thanks


  • Patrick Steele

    #2
    Re: Iterating through a ListBox in VB.NET VS 2005

    In article <#dG2BOl7GHA.12 56@TK2MSFTNGP04 .phx.gbl>, henry@yada.com
    says...
    I have a listbox on a Windows form and it is bound using the following code:
    >
    objListBox.Data Source = tbl
    >
    objListBox.Disp layMember = "ContactNam e"
    >
    objListBox.Valu eMember = "ContactID"
    >
    ------------------------------------
    >
    On my form, I have a multi-select listbox and I would like to select a few
    items. So If my listbox contains: CA, NV, OR, WA, AZ
    >
    and I choose CA and NV,
    >
    >
    >
    How can I get get those items?
    >
    I tried something like
    >
    dim itm as object
    >
    for each itm in objlistbox.sele cteditems
    >
    console.writeli ne(itm)
    >
    next
    >
    >
    >
    but it doesn't work.
    What do you mean "it doesn't work"? Do you get an error?

    --
    Patrick Steele

    Comment

    • Henry Jones

      #3
      Re: Iterating through a ListBox in VB.NET VS 2005

      I get the following error:
      Argument 'Prompt' cannot be converted to type 'String'.

      but I have tried other things and they don't work also.

      For example I tried console.writeli ne(objlist.sele cteditem) and tried to
      list objlist.text and many other things but just can't display the State or
      it's value.

      If I could see some working code on how to do this that would be great. I'm
      not necessarily in need of fixing what I have, I would like to get an
      example of something that someone already has that works.

      Thanks.


      "Patrick Steele" <patrick@mvps.o rgwrote in message
      news:MPG.1f989a cbef6948db9896b 0@msnews.micros oft.com...
      In article <#dG2BOl7GHA.12 56@TK2MSFTNGP04 .phx.gbl>, henry@yada.com
      says...
      >I have a listbox on a Windows form and it is bound using the following
      >code:
      >>
      >objListBox.Dat aSource = tbl
      >>
      >objListBox.Dis playMember = "ContactNam e"
      >>
      >objListBox.Val ueMember = "ContactID"
      >>
      >------------------------------------
      >>
      >On my form, I have a multi-select listbox and I would like to select a
      >few
      >items. So If my listbox contains: CA, NV, OR, WA, AZ
      >>
      >and I choose CA and NV,
      >>
      >>
      >>
      >How can I get get those items?
      >>
      >I tried something like
      >>
      >dim itm as object
      >>
      >for each itm in objlistbox.sele cteditems
      >>
      >console.writel ine(itm)
      >>
      >next
      >>
      >>
      >>
      >but it doesn't work.
      >
      What do you mean "it doesn't work"? Do you get an error?
      >
      --
      Patrick Steele
      http://weblogs.asp.net/psteele

      Comment

      • Stephany Young

        #4
        Re: Iterating through a ListBox in VB.NET VS 2005

        You are on the right track, but you have to remember that the SelectedItems
        collection is a collection of ListItems.

        Each selected item is a ListItem that contains a string and therfore you
        need to 'turn' the ListItem into a string before you can display the correct
        value.

        console.writeli ne(ctype(itm, string))


        "Henry Jones" <henry@yada.com wrote in message
        news:%23dG2BOl7 GHA.1256@TK2MSF TNGP04.phx.gbl. ..
        >I have a listbox on a Windows form and it is bound using the following
        >code:
        >
        objListBox.Data Source = tbl
        >
        objListBox.Disp layMember = "ContactNam e"
        >
        objListBox.Valu eMember = "ContactID"
        >
        ------------------------------------
        >
        On my form, I have a multi-select listbox and I would like to select a few
        items. So If my listbox contains: CA, NV, OR, WA, AZ
        >
        and I choose CA and NV,
        >
        >
        >
        How can I get get those items?
        >
        I tried something like
        >
        dim itm as object
        >
        for each itm in objlistbox.sele cteditems
        >
        console.writeli ne(itm)
        >
        next
        >
        >
        >
        but it doesn't work. Why is this so hard to get the selected items in a
        listbox?
        >
        >
        >
        Thanks
        >
        >

        Comment

        • Kerry Moorman

          #5
          Re: Iterating through a ListBox in VB.NET VS 2005

          Henry,

          This works for me in VS2003:

          for each itm as DataRowView in objlistbox.sele cteditems

          console.writeli ne(itm("Contact Name"))

          next

          Kerry Moorman


          "Henry Jones" wrote:
          I get the following error:
          Argument 'Prompt' cannot be converted to type 'String'.
          >
          but I have tried other things and they don't work also.
          >
          For example I tried console.writeli ne(objlist.sele cteditem) and tried to
          list objlist.text and many other things but just can't display the State or
          it's value.
          >
          If I could see some working code on how to do this that would be great. I'm
          not necessarily in need of fixing what I have, I would like to get an
          example of something that someone already has that works.
          >
          Thanks.
          >
          >
          "Patrick Steele" <patrick@mvps.o rgwrote in message
          news:MPG.1f989a cbef6948db9896b 0@msnews.micros oft.com...
          In article <#dG2BOl7GHA.12 56@TK2MSFTNGP04 .phx.gbl>, henry@yada.com
          says...
          I have a listbox on a Windows form and it is bound using the following
          code:
          >
          objListBox.Data Source = tbl
          >
          objListBox.Disp layMember = "ContactNam e"
          >
          objListBox.Valu eMember = "ContactID"
          >
          ------------------------------------
          >
          On my form, I have a multi-select listbox and I would like to select a
          few
          items. So If my listbox contains: CA, NV, OR, WA, AZ
          >
          and I choose CA and NV,
          >
          >
          >
          How can I get get those items?
          >
          I tried something like
          >
          dim itm as object
          >
          for each itm in objlistbox.sele cteditems
          >
          console.writeli ne(itm)
          >
          next
          >
          >
          >
          but it doesn't work.
          What do you mean "it doesn't work"? Do you get an error?

          --
          Patrick Steele
          http://weblogs.asp.net/psteele
          >
          >
          >

          Comment

          • Stephany Young

            #6
            Re: Iterating through a ListBox in VB.NET VS 2005

            Sorry, I didn't read you post properly.

            Because your ListBox is bound, each item is a reference to a row in tbl.

            In this case you need to use SelectedIndicie s instead of SelectedItems and
            retrieve the value from tbl:

            For _i as Integer = 0 to objlistbox.Sele ctedIndicies.Co unt
            Console.WriteLi ne(tbl.Rows(obj listbox.Selecte dIndicies(_i))( "ContactNam e"))
            Next

            Each item in SelectedIndicie s effectively gives you the row number in the
            source.


            "Henry Jones" <henry@yada.com wrote in message
            news:%23dG2BOl7 GHA.1256@TK2MSF TNGP04.phx.gbl. ..
            >I have a listbox on a Windows form and it is bound using the following
            >code:
            >
            objListBox.Data Source = tbl
            >
            objListBox.Disp layMember = "ContactNam e"
            >
            objListBox.Valu eMember = "ContactID"
            >
            ------------------------------------
            >
            On my form, I have a multi-select listbox and I would like to select a few
            items. So If my listbox contains: CA, NV, OR, WA, AZ
            >
            and I choose CA and NV,
            >
            >
            >
            How can I get get those items?
            >
            I tried something like
            >
            dim itm as object
            >
            for each itm in objlistbox.sele cteditems
            >
            console.writeli ne(itm)
            >
            next
            >
            >
            >
            but it doesn't work. Why is this so hard to get the selected items in a
            listbox?
            >
            >
            >
            Thanks
            >
            >

            Comment

            • Henry Jones

              #7
              Thanks for the replies

              Thanks for the code to iterate through the listbox. Your suggestions
              worked!

              H


              "Henry Jones" <henry@yada.com wrote in message
              news:%23dG2BOl7 GHA.1256@TK2MSF TNGP04.phx.gbl. ..
              >I have a listbox on a Windows form and it is bound using the following
              >code:
              >
              objListBox.Data Source = tbl
              >
              objListBox.Disp layMember = "ContactNam e"
              >
              objListBox.Valu eMember = "ContactID"
              >
              ------------------------------------
              >
              On my form, I have a multi-select listbox and I would like to select a few
              items. So If my listbox contains: CA, NV, OR, WA, AZ
              >
              and I choose CA and NV,
              >
              >
              >
              How can I get get those items?
              >
              I tried something like
              >
              dim itm as object
              >
              for each itm in objlistbox.sele cteditems
              >
              console.writeli ne(itm)
              >
              next
              >
              >
              >
              but it doesn't work. Why is this so hard to get the selected items in a
              listbox?
              >
              >
              >
              Thanks
              >
              >

              Comment

              Working...