loop through listbox values

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

    #1

    loop through listbox values

    Hi,

    How do I loop through listbox items?

    Thanks,

    yue
  • Sam Sungshik Kong

    #2
    Re: loop through listbox values

    This will serve.

    foreach(object o in listBox1.Items)

    Sam


    "yue" <yue@mail.com > wrote in message
    news:0c7501c356 ae$652f1af0$a60 1280a@phx.gbl.. .[color=blue]
    > Hi,
    >
    > How do I loop through listbox items?
    >
    > Thanks,
    >
    > yue[/color]


    Comment

    • yue

      #3
      Re: loop through listbox values

      Hi,

      That did not work also.

      yue[color=blue]
      >-----Original Message-----
      >try:
      >
      >foreach (string s in listBox1.Items)
      >
      >"yue" <yue@hotmail.co m> wrote in message
      >news:4d5d01c35 6b4$b73a7730$a0 01280a@phx.gbl. ..[color=green]
      >> Hi Sam,
      >> i have done this but cant get the row values.
      >>
      >> Please help,
      >>
      >> thanks,
      >>
      >> yue[color=darkred]
      >> >-----Original Message-----
      >> >This will serve.
      >> >
      >> > foreach(object o in listBox1.Items)
      >> >
      >> >Sam
      >> >
      >> >
      >> >"yue" <yue@mail.com > wrote in message
      >> >news:0c7501c35 6ae$652f1af0$a6 01280a@phx.gbl. ..
      >> >> Hi,
      >> >>
      >> >> How do I loop through listbox items?
      >> >>
      >> >> Thanks,
      >> >>
      >> >> yue
      >> >
      >> >
      >> >.
      >> >[/color][/color]
      >
      >
      >.
      >[/color]

      Comment

      • Chris Capel

        #4
        Re: loop through listbox values

        ListBox items accept objects of any type as elements. Find where the
        elements are being added, and use that class in the foreach. For instance,
        if you do this

        listBox1.Items. Add(new MyCustomClass(" Hi", 3, true));

        then you would do this to get the values

        foreach (MyCustomClass item in listBox1.Items) {
        }

        Chris

        "yue" <yue@mail.com > wrote in message
        news:0c7501c356 ae$652f1af0$a60 1280a@phx.gbl.. .[color=blue]
        > Hi,
        >
        > How do I loop through listbox items?
        >
        > Thanks,
        >
        > yue[/color]


        Comment

        • yue

          #5
          Re: loop through listbox values

          Chris,
          This is good if you use the add method of the listbox.
          I set the listbox Datasource on a filtered DataTable.
          This is why i need to loop through the listbox and get the
          elements.

          Is there no easy way to do this? This is so frustrating to
          me...

          yue[color=blue]
          >-----Original Message-----
          >ListBox items accept objects of any type as elements.[/color]
          Find where the[color=blue]
          >elements are being added, and use that class in the[/color]
          foreach. For instance,[color=blue]
          >if you do this
          >
          >listBox1.Items .Add(new MyCustomClass(" Hi", 3, true));
          >
          >then you would do this to get the values
          >
          >foreach (MyCustomClass item in listBox1.Items) {
          >}
          >
          >Chris
          >
          >"yue" <yue@mail.com > wrote in message
          >news:0c7501c35 6ae$652f1af0$a6 01280a@phx.gbl. ..[color=green]
          >> Hi,
          >>
          >> How do I loop through listbox items?
          >>
          >> Thanks,
          >>
          >> yue[/color]
          >
          >
          >.
          >[/color]

          Comment

          • Chris Capel

            #6
            Re: loop through listbox values

            In this case, you ought to loop through the datasource instead of the list
            box's collection. For a DataTable, this is

            foreach (DataRow row in dataTable1.Rows ) {}

            It ought to be similar for a DataView.

            Chris

            "yue" <yue@hotmail.co m> wrote in message
            news:4ebd01c356 be$4e392a10$a00 1280a@phx.gbl.. .[color=blue]
            > Chris,
            > This is good if you use the add method of the listbox.
            > I set the listbox Datasource on a filtered DataTable.
            > This is why i need to loop through the listbox and get the
            > elements.
            >
            > Is there no easy way to do this? This is so frustrating to
            > me...
            >
            > yue[color=green]
            > >-----Original Message-----
            > >ListBox items accept objects of any type as elements.[/color]
            > Find where the[color=green]
            > >elements are being added, and use that class in the[/color]
            > foreach. For instance,[color=green]
            > >if you do this
            > >
            > >listBox1.Items .Add(new MyCustomClass(" Hi", 3, true));
            > >
            > >then you would do this to get the values
            > >
            > >foreach (MyCustomClass item in listBox1.Items) {
            > >}
            > >
            > >Chris
            > >
            > >"yue" <yue@mail.com > wrote in message
            > >news:0c7501c35 6ae$652f1af0$a6 01280a@phx.gbl. ..[color=darkred]
            > >> Hi,
            > >>
            > >> How do I loop through listbox items?
            > >>
            > >> Thanks,
            > >>
            > >> yue[/color]
            > >
            > >
            > >.
            > >[/color][/color]


            Comment

            • yue

              #7
              Re: loop through listbox values

              Chris,

              Thank You for all your help.

              I will try this.

              yue[color=blue]
              >-----Original Message-----
              >In this case, you ought to loop through the datasource[/color]
              instead of the list[color=blue]
              >box's collection. For a DataTable, this is
              >
              >foreach (DataRow row in dataTable1.Rows ) {}
              >
              >It ought to be similar for a DataView.
              >
              >Chris
              >
              >"yue" <yue@hotmail.co m> wrote in message
              >news:4ebd01c35 6be$4e392a10$a0 01280a@phx.gbl. ..[color=green]
              >> Chris,
              >> This is good if you use the add method of the listbox.
              >> I set the listbox Datasource on a filtered DataTable.
              >> This is why i need to loop through the listbox and get[/color][/color]
              the[color=blue][color=green]
              >> elements.
              >>
              >> Is there no easy way to do this? This is so frustrating[/color][/color]
              to[color=blue][color=green]
              >> me...
              >>
              >> yue[color=darkred]
              >> >-----Original Message-----
              >> >ListBox items accept objects of any type as elements.[/color]
              >> Find where the[color=darkred]
              >> >elements are being added, and use that class in the[/color]
              >> foreach. For instance,[color=darkred]
              >> >if you do this
              >> >
              >> >listBox1.Items .Add(new MyCustomClass(" Hi", 3, true));
              >> >
              >> >then you would do this to get the values
              >> >
              >> >foreach (MyCustomClass item in listBox1.Items) {
              >> >}
              >> >
              >> >Chris
              >> >
              >> >"yue" <yue@mail.com > wrote in message
              >> >news:0c7501c35 6ae$652f1af0$a6 01280a@phx.gbl. ..
              >> >> Hi,
              >> >>
              >> >> How do I loop through listbox items?
              >> >>
              >> >> Thanks,
              >> >>
              >> >> yue
              >> >
              >> >
              >> >.
              >> >[/color][/color]
              >
              >
              >.
              >[/color]

              Comment

              • Gerald Lightsey

                #8
                Re: loop through listbox values

                On Wed, 30 Jul 2003 08:22:31 -0700, in the
                microsoft.publi c.dotnet.langua ges.csharp group, yue said...[color=blue]
                > How do I loop through listbox items?[/color]

                Items is a property of a listBox containing a collection. If you follow
                the links from ListBox Class to ListBox Members to Items (inherited from
                ListControl) to ListControl.Ite ms Property you will find one example of
                how to iterate through the list.

                In addition, follow through from CollectionBaseC lass, CollectionBase
                Members to GetEnumerator which describes also MoveNext, Current and
                Reset.

                So essentially you have choices of for, foreach and GetEnumerator
                approaches to do what you want to do.

                Gerald

                Comment

                Working...