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. Visual Basic
  4. For and next loop... problem

For and next loop... problem

Scheduled Pinned Locked Moved Visual Basic
databasehelpquestion
10 Posts 3 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.
  • S Offline
    S Offline
    smguc
    wrote on last edited by
    #1

    Hi, I am trying to insert selected items from a listbox into a label, the listbox is fillby a database query, the problem is all I get is this System.Data.DataRowView it works fine when I dont use a database query and just text in the listbox, but need to use a database query. Can anyone help on a solution for this?? Dim i As Integer For i = 0 To ListBoxUpgrade.Items.Count - 1 If ListBoxUpgrade.Items(i) Then LabelSum.Text = LabelSum.Text & Space(6) & ListBoxUpgrade.SelectedItem(i) & vbCrLf End If Next i Thanks in advance! Stefan.

    "All answers have a question? All code has an end."

    L T 2 Replies Last reply
    0
    • S smguc

      Hi, I am trying to insert selected items from a listbox into a label, the listbox is fillby a database query, the problem is all I get is this System.Data.DataRowView it works fine when I dont use a database query and just text in the listbox, but need to use a database query. Can anyone help on a solution for this?? Dim i As Integer For i = 0 To ListBoxUpgrade.Items.Count - 1 If ListBoxUpgrade.Items(i) Then LabelSum.Text = LabelSum.Text & Space(6) & ListBoxUpgrade.SelectedItem(i) & vbCrLf End If Next i Thanks in advance! Stefan.

      "All answers have a question? All code has an end."

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

      smguc wrote:

      ListBoxUpgrade.SelectedItem(i)

      try ListBoxUpgrade.SelectedItem(i).Text instead without .Text the ToString() method gets called, which by default returns a string naming the type. :)

      Luc Pattyn


      try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


      S 1 Reply Last reply
      0
      • L Luc Pattyn

        smguc wrote:

        ListBoxUpgrade.SelectedItem(i)

        try ListBoxUpgrade.SelectedItem(i).Text instead without .Text the ToString() method gets called, which by default returns a string naming the type. :)

        Luc Pattyn


        try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


        S Offline
        S Offline
        smguc
        wrote on last edited by
        #3

        Hi Luc, Thanks for that, but doesn't accept the .text after the ListBoxUpgrade.SelectedItem(i) so frustrading it works in 'VB6' which is where I have upgraded from to 'VB2005' all because of 'Vista', many different rules and changes. What I am trying to achieve, is there is a list of items in a listbox which you can multi select and from there show only those selected items in a label as a summary 'like a report'. Any more suggestions would be much appreciated! Thanks, Stefan.

        "All answers have a question? All code has an end."

        L 1 Reply Last reply
        0
        • S smguc

          Hi Luc, Thanks for that, but doesn't accept the .text after the ListBoxUpgrade.SelectedItem(i) so frustrading it works in 'VB6' which is where I have upgraded from to 'VB2005' all because of 'Vista', many different rules and changes. What I am trying to achieve, is there is a list of items in a listbox which you can multi select and from there show only those selected items in a label as a summary 'like a report'. Any more suggestions would be much appreciated! Thanks, Stefan.

          "All answers have a question? All code has an end."

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

          I missed that you missed an 's' try ListBoxUpgrade.SelectedItems(i).Text instead :)

          Luc Pattyn


          try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


          S 1 Reply Last reply
          0
          • L Luc Pattyn

            I missed that you missed an 's' try ListBoxUpgrade.SelectedItems(i).Text instead :)

            Luc Pattyn


            try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


            S Offline
            S Offline
            smguc
            wrote on last edited by
            #5

            Hmmm... No still no luck! Doesn't like the .text after it. Sometimes makes me think I should go back to VB6, haha. Was like my second lanugage! This is the code for VB6, which works fine the only difference is that the 'i' was declare as a variant, but VB2005 wants it as a object, which didn't work so changed it to a Integer. Dim i As Variant For i = 0 To ListBoxUpgrade.ListCount - 1 If ListBoxUpgrade.Selected(i) Then LabelSum = LabelSum & Space(6) & ListBoxUpgrade.List(i, 3) & vbCrLf End If Next i Do you have any more ideas or a better way to write the code?? Or go back to VB6... lol Thanks, Stefan.

            "All answers have a question? All code has an end."

            L 1 Reply Last reply
            0
            • S smguc

              Hmmm... No still no luck! Doesn't like the .text after it. Sometimes makes me think I should go back to VB6, haha. Was like my second lanugage! This is the code for VB6, which works fine the only difference is that the 'i' was declare as a variant, but VB2005 wants it as a object, which didn't work so changed it to a Integer. Dim i As Variant For i = 0 To ListBoxUpgrade.ListCount - 1 If ListBoxUpgrade.Selected(i) Then LabelSum = LabelSum & Space(6) & ListBoxUpgrade.List(i, 3) & vbCrLf End If Next i Do you have any more ideas or a better way to write the code?? Or go back to VB6... lol Thanks, Stefan.

              "All answers have a question? All code has an end."

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

              Dont go back to VB6, if anything show your current VB.NET code here. And tell exactly what the current result is and what it should be. :)

              Luc Pattyn


              try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


              1 Reply Last reply
              0
              • S smguc

                Hi, I am trying to insert selected items from a listbox into a label, the listbox is fillby a database query, the problem is all I get is this System.Data.DataRowView it works fine when I dont use a database query and just text in the listbox, but need to use a database query. Can anyone help on a solution for this?? Dim i As Integer For i = 0 To ListBoxUpgrade.Items.Count - 1 If ListBoxUpgrade.Items(i) Then LabelSum.Text = LabelSum.Text & Space(6) & ListBoxUpgrade.SelectedItem(i) & vbCrLf End If Next i Thanks in advance! Stefan.

                "All answers have a question? All code has an end."

                T Offline
                T Offline
                TwoFaced
                wrote on last edited by
                #7

                The listbox contains objects which could be anything. ListBoxUpgrade.SelectedItem(i) will return whatever text the ToString method returns. For most objects this is the type of object it is. Because you've bound your listview you actually have a listview full of DataRowView objects. This is why the ToString method is returning System.Data.DataRowView. If you want to get the corresponding text of the item (the text being displayed in the listbox) you can use the GetItemText method.

                    For Each obj As Object In ListBox1.SelectedItems
                        Console.WriteLine(ListBox1.GetItemText(obj))
                    Next
                
                L S 2 Replies Last reply
                0
                • T TwoFaced

                  The listbox contains objects which could be anything. ListBoxUpgrade.SelectedItem(i) will return whatever text the ToString method returns. For most objects this is the type of object it is. Because you've bound your listview you actually have a listview full of DataRowView objects. This is why the ToString method is returning System.Data.DataRowView. If you want to get the corresponding text of the item (the text being displayed in the listbox) you can use the GetItemText method.

                      For Each obj As Object In ListBox1.SelectedItems
                          Console.WriteLine(ListBox1.GetItemText(obj))
                      Next
                  
                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  Hi, that looks great. Never knew VB code could look so clean. :)

                  Luc Pattyn


                  try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                  1 Reply Last reply
                  0
                  • T TwoFaced

                    The listbox contains objects which could be anything. ListBoxUpgrade.SelectedItem(i) will return whatever text the ToString method returns. For most objects this is the type of object it is. Because you've bound your listview you actually have a listview full of DataRowView objects. This is why the ToString method is returning System.Data.DataRowView. If you want to get the corresponding text of the item (the text being displayed in the listbox) you can use the GetItemText method.

                        For Each obj As Object In ListBox1.SelectedItems
                            Console.WriteLine(ListBox1.GetItemText(obj))
                        Next
                    
                    S Offline
                    S Offline
                    smguc
                    wrote on last edited by
                    #9

                    Excellent! Thank you very much! You would not believe the amount of books I have, and read that doesn't even state the 'GetItemText' method, Have only just made the transistion in VB2005 a few weeks now. Great help! Thanks again.:) Stefan.

                    "All answers have a question? All code has an end."

                    T 1 Reply Last reply
                    0
                    • S smguc

                      Excellent! Thank you very much! You would not believe the amount of books I have, and read that doesn't even state the 'GetItemText' method, Have only just made the transistion in VB2005 a few weeks now. Great help! Thanks again.:) Stefan.

                      "All answers have a question? All code has an end."

                      T Offline
                      T Offline
                      TwoFaced
                      wrote on last edited by
                      #10

                      That's what IntelliSense is for :) Also the MSDN library is a big help.

                      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