Listview: add new row

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • donkeyparfait
    New Member
    • Jan 2007
    • 10

    Listview: add new row

    I am writing code to display in a listview. I have used a For/Next loop that should add a new line in the listview each time it loops. All i get now is one line when i execute the code. I tested the loop by sending the output to a message box and it showed a message box for every loop. So the loop works, but it is not displaying it in the listview correctly. I think I am missing something that would make the loop process go to the next row in the listview. Here is the code i have so far.

    Dim intMultiplicand , intMultiplier, intProduct As Integer
    Dim lviTable As New ListViewItem
    Const strX = "X"
    Const strEquals = "="
    Dim objForm1 As New Form1

    intMultiplicand = 1
    intMultiplier = 1
    For intMultiplicand = 1 To 12
    intProduct = intMultiplicand * intMultiplier
    lviTable.SubIte ms.Add(intMulti plicand)
    lviTable.SubIte ms.Add(strX)
    lviTable.SubIte ms.Add(intMulti plier)
    lviTable.SubIte ms.Add(strEqual s)
    lviTable.SubIte ms.Add(intProdu ct)

    For intMultiplier = 1 To 12
    intProduct = intMultiplicand * intMultiplier
    lviTable.SubIte ms.Add(intMulti plicand)
    lviTable.SubIte ms.Add(strX)
    lviTable.SubIte ms.Add(intMulti plier)
    lviTable.SubIte ms.Add(strEqual s)
    lviTable.SubIte ms.Add(intProdu ct)

    Next intMultiplier
    Next intMultiplicand

    lstTable.Items. Add(lviTable)

    Please help.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    I'm not that familiar with the listview, so don't take anything I say too seriously. But it looks to me as though all you're doing in these loops is adding lots of subitems (which I believe are equivalent to "columns") into one row.

    I didn't really follow the code very well, but if you're expecting multiple rows, then wouldn't I expect to find the Items.Add inside the loop(s)?

    (I must have a different version of the listview control here, as it doesn't even have .Items - it has .ListItems. So I may be talking complete garbage. :))

    Comment

    • donkeyparfait
      New Member
      • Jan 2007
      • 10

      #3
      Originally posted by Killer42
      I'm not that familiar with the listview, so don't take anything I say too seriously. But it looks to me as though all you're doing in these loops is adding lots of subitems (which I believe are equivalent to "columns") into one row.

      I didn't really follow the code very well, but if you're expecting multiple rows, then wouldn't I expect to find the Items.Add inside the loop(s)?

      (I must have a different version of the listview control here, as it doesn't even have .Items - it has .ListItems. So I may be talking complete garbage. :))

      Good point. The loop is not adding any new Items. The assignment requires the subitems, but you are right. I need something that will add Items in the loop. Thanks. This gives me something to work with now. I have been staring at this code all weekend. You guys are great!

      Comment

      • donkeyparfait
        New Member
        • Jan 2007
        • 10

        #4
        Originally posted by donkeyparfait
        Good point. The loop is not adding any new Items. The assignment requires the subitems, but you are right. I need something that will add Items in the loop. Thanks. This gives me something to work with now. I have been staring at this code all weekend. You guys are great!

        BTW,

        The code is supposed to produce the multiplication table throught the 12's in the listview. It should look like this...

        1 X 1 = 1
        1 X 2 = 2
        1 X 3 = 3

        There should be 144 Items when it is finished, and it should all populate at the touch of one button.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Hm... in that case, could you see whether this works?
          Code:
          Dim intMultiplicand, intMultiplier, intProduct As Integer
          Dim lviTable As New ListViewItem
          Const strX = "X"
          Const strEquals = "="
          ' Dim objForm1 As New Form1
          
          ' intMultiplicand = 1
          ' intMultiplier = 1
          For intMultiplicand = 1 To 12
            ' intProduct = intMultiplicand * intMultiplier
            ' lviTable.SubItems.Add(intMultiplicand)
            ' lviTable.SubItems.Add(strX)
            ' lviTable.SubItems.Add(intMultiplier)
            ' lviTable.SubItems.Add(strEquals)
            ' lviTable.SubItems.Add(intProduct)
            For intMultiplier = 1 To 12
              
              [B]' Somehow clear/reset lviTable. I don't know how.[/B]
              
              intProduct = intMultiplicand * intMultiplier
              lviTable.SubItems.Add (intMultiplicand)
              lviTable.SubItems.Add (strX)
              lviTable.SubItems.Add (intMultiplier)
              lviTable.SubItems.Add (strEquals)
              lviTable.SubItems.Add (intProduct)
              [B]lstTable.Items.Add (lviTable)[/B]
            Next intMultiplier
          Next intMultiplicand

          Comment

          • donkeyparfait
            New Member
            • Jan 2007
            • 10

            #6
            I was thinking the same thing, but I got this error when I tried that.

            An unhandled exception of type 'System.Argumen tException' occurred in system.windows. forms.dll

            Additional information: Cannot add or insert the item '' in more than one place. You must first remove it from its current location or clone it.

            What does that mean?

            Comment

            • donkeyparfait
              New Member
              • Jan 2007
              • 10

              #7
              Now I am getting 144 Items, but it is not displaying the subitems. Here is what I have changed.

              Code:
                Dim intMultiplicand, intMultiplier, intProduct As Integer
                      Dim lviTable As New ListViewItem
                      Const strX = "X"
                      Const strEquals = "="
                      Dim objForm1 As New Form1
              
                      intMultiplier = 1
                      For intMultiplicand = 1 To 12
                          [B]lstTable.Items.Add(intMultiplicand)[/B]          
                         intProduct = intMultiplicand * intMultiplier
                          lviTable.SubItems.Add(strX)
                          lviTable.SubItems.Add(intMultiplier)
                          lviTable.SubItems.Add(strEquals)
                          lviTable.SubItems.Add(intProduct)
                          For intMultiplier = 2 To 12
                             [B] lstTable.Items.Add(intMultiplicand)[/B]
                              intProduct = intMultiplicand * intMultiplier
                              lviTable.SubItems.Add(strX)
                              lviTable.SubItems.Add(intMultiplier)
                              lviTable.SubItems.Add(strEquals)
                              lviTable.SubItems.Add(intProduct)
              
                          Next intMultiplier
                      Next intMultiplicand
              
                      lstTable.Items.Add(lviTable)

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by donkeyparfait
                I was thinking the same thing, but I got this error when I tried that.

                An unhandled exception of type 'System.Argumen tException' occurred in system.windows. forms.dll
                Additional information: Cannot add or insert the item '' in more than one place. You must first remove it from its current location or clone it.


                What does that mean?
                I think that means you tried to add two items with the same index. To me, that sounds as though you are heading in the right direction - after all, at least you are trying to add more than one item. :)

                You might try just plugging in some unique string (Eg. the two numbers concatenated) as the index, and see what happens. I wish I had more experience with the listview...

                Comment

                • donkeyparfait
                  New Member
                  • Jan 2007
                  • 10

                  #9
                  You have been a great help. At least I am getting new results now. Thanks.

                  Comment

                  • senthilk111
                    New Member
                    • Jan 2007
                    • 9

                    #10
                    hello, you just trying load mutiplication table in the listview. In vb 6.0 no need to new operator for listview. and please surf in msdn for listview in vb 6.0 you get good description.

                    if you have any doubt in that example you can query me

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      And make sure you let us know here, how things turn out. :)

                      Comment

                      • donkeyparfait
                        New Member
                        • Jan 2007
                        • 10

                        #12
                        THAT WAS IT!!!!!!! You can add subItems without declaring a variable for the ListViewItems. Here is the solution:

                        Code:
                         Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
                                Dim intMultiplicand, intMultiplier, intProduct As Integer
                                Const strX = "X"
                                Const strEquals = "="
                                Dim objForm1 As New Form1
                                Dim intCount As Integer
                        
                        
                               [B] intCount = 0[/B]       
                                    For intMultiplicand = 1 To 12
                                    intMultiplier = 1
                                    lstTable.Items.Add(intMultiplicand)
                                    intProduct = intMultiplicand * intMultiplier
                                    [B]lstTable.Items(intCount).[/B]SubItems.Add(strX)
                                    [B]lstTable.Items(intCount).[/B]SubItems.Add(intMultiplier)
                                    [B]lstTable.Items(intCount).[/B]SubItems.Add(strEquals)
                                    [B]lstTable.Items(intCount).[/B]SubItems.Add(intProduct)
                                    [B]intCount += 1[/B]
                                    For intMultiplier = 2 To 12
                                        lstTable.Items.Add(intMultiplicand)
                                        intProduct = intMultiplicand * intMultiplier
                                        [B]lstTable.Items(intCount).[/B]SubItems.Add(strX)
                                        [B]lstTable.Items(intCount).[/B]SubItems.Add(intMultiplier)
                                        [B]lstTable.Items(intCount).[/B]SubItems.Add(strEquals)
                                        [B]lstTable.Items(intCount).[/B]SubItems.Add(intProduct)
                                        [B]intCount += 1[/B]           
                                     Next intMultiplier
                                Next intMultiplicand
                        I added a count in the loop that would add one every loop so i could tell the listview where to place the Item and SubItem. Then I changed my lsvTable.SubIte ms.Add() to lstTable.Items( intCount).SubIt ems.Add. I'm not sure if i explained this correctly, but THANK YOU GUYS VERY MUCH. IT WORKED.

                        Comment

                        • Killer42
                          Recognized Expert Expert
                          • Oct 2006
                          • 8429

                          #13
                          Excellent!

                          It's good to see you've resolved your problem.

                          Comment

                          Working...