Results 1 to 18 of 18

Thread: For...Next loop nested in another For...Next loop [RESOLVED]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131

    For...Next loop nested in another For...Next loop [RESOLVED]

    I know exactly what I want to do but don't know how to do it. I want to create a table of 144 rows in ListView control. I have created five columns: multiplicand, X, multiplier, =, and product. This table is going to be a Multiplication table where both the Multiplicand and the Mulitplier vary from 1 through 12, hence 144 rows. I want option strict on and the processing to occur in the form load event only. Anyone got an idea of how to write a Nested For...Next loop for this??
    Last edited by twisted; Jun 23rd, 2004 at 09:39 AM.
    Twisted

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    What is the processing you want to accomplish? Filling the listview? Calculating the product? Something else?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    As soon as the form loads I want the Multiplication Table to display in Listview. I want no user input at all, just a big table with 144 rows like this:

    1 X 1 = 1
    1 X 2 = 2
    1 X 3 = 3
    1 X 4 = 4
    etc...

    the only things I will use for this program is looping and the list view box. Here is my interface if you want to see what I want it to look like. My brother said the best way is to use a For...Next loop nested in another For...Next loop. This is all the code for the program, but I'm illerate when it comes to Nested loops.

    VB Code:
    1. Option Strict On
    2. Public Class Form1
    3.     Inherits System.Windows.Forms.Form
    4.  
    5. #Region " Windows Form Designer generated code "
    6.  
    7.     Public Sub New()
    8.         MyBase.New()
    9.  
    10.         'This call is required by the Windows Form Designer.
    11.         InitializeComponent()
    12.  
    13.         'Add any initialization after the InitializeComponent() call
    14.  
    15.     End Sub
    16.  
    17.     'Form overrides dispose to clean up the component list.
    18.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    19.         If disposing Then
    20.             If Not (components Is Nothing) Then
    21.                 components.Dispose()
    22.             End If
    23.         End If
    24.         MyBase.Dispose(disposing)
    25.     End Sub
    26.  
    27.     'Required by the Windows Form Designer
    28.     Private components As System.ComponentModel.IContainer
    29.  
    30.     'NOTE: The following procedure is required by the Windows Form Designer
    31.     'It can be modified using the Windows Form Designer.  
    32.     'Do not modify it using the code editor.
    33.     Friend WithEvents ListView1 As System.Windows.Forms.ListView
    34.     Friend WithEvents colMultiplicand As System.Windows.Forms.ColumnHeader
    35.     Friend WithEvents colX As System.Windows.Forms.ColumnHeader
    36.     Friend WithEvents colMultiplier As System.Windows.Forms.ColumnHeader
    37.     Friend WithEvents colEqual As System.Windows.Forms.ColumnHeader
    38.     Friend WithEvents colProduct As System.Windows.Forms.ColumnHeader
    39.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    40.         Me.ListView1 = New System.Windows.Forms.ListView
    41.         Me.colMultiplicand = New System.Windows.Forms.ColumnHeader
    42.         Me.colX = New System.Windows.Forms.ColumnHeader
    43.         Me.colMultiplier = New System.Windows.Forms.ColumnHeader
    44.         Me.colEqual = New System.Windows.Forms.ColumnHeader
    45.         Me.colProduct = New System.Windows.Forms.ColumnHeader
    46.         Me.SuspendLayout()
    47.         '
    48.         'ListView1
    49.         '
    50.         Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.colMultiplicand, Me.colX, Me.colMultiplier, Me.colEqual, Me.colProduct})
    51.         Me.ListView1.Location = New System.Drawing.Point(0, 0)
    52.         Me.ListView1.Name = "ListView1"
    53.         Me.ListView1.Size = New System.Drawing.Size(304, 344)
    54.         Me.ListView1.TabIndex = 0
    55.         Me.ListView1.View = System.Windows.Forms.View.Details
    56.         '
    57.         'colMultiplicand
    58.         '
    59.         Me.colMultiplicand.Text = "Multiplicand"
    60.         Me.colMultiplicand.Width = 68
    61.         '
    62.         'colX
    63.         '
    64.         Me.colX.Text = "X"
    65.         Me.colX.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
    66.         Me.colX.Width = 19
    67.         '
    68.         'colMultiplier
    69.         '
    70.         Me.colMultiplier.Text = "Multiplier"
    71.         Me.colMultiplier.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
    72.         Me.colMultiplier.Width = 53
    73.         '
    74.         'colEqual
    75.         '
    76.         Me.colEqual.Text = "="
    77.         Me.colEqual.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
    78.         Me.colEqual.Width = 18
    79.         '
    80.         'colProduct
    81.         '
    82.         Me.colProduct.Text = "Product"
    83.         Me.colProduct.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
    84.         Me.colProduct.Width = 79
    85.         '
    86.         'Form1
    87.         '
    88.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    89.         Me.ClientSize = New System.Drawing.Size(232, 326)
    90.         Me.Controls.Add(Me.ListView1)
    91.         Me.Name = "Form1"
    92.         Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
    93.         Me.Text = "Form1"
    94.         Me.ResumeLayout(False)
    95.  
    96.     End Sub
    97.  
    98. #End Region
    99.  
    100.     Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
    101.  
    102.     End Sub
    103. End Class
    Last edited by twisted; Jun 22nd, 2004 at 09:32 AM.
    Twisted

  4. #4
    Hyperactive Member dogfish227's Avatar
    Join Date
    Oct 2002
    Location
    GA
    Posts
    409
    VB Code:
    1. dim num1 as integer
    2. dim num2 as integer
    3.  
    4.  
    5. for num1 = 1 to 12
    6.    for num2 = 1 to 12
    7.       'the first time            num1=1  num2=1
    8.       'secound time           num1=1  num2=2
    9.       '                                num1=1  num2=3
    10.       'it will go lie that till  num1=1  num2=12                
    11.       ' then                       num1=2  num2=1
    12.                                       num1=2  num2=2
    13.                                       num1=2  num2=3  
    14.                        
    15.    next
    16. next

    do you see how they work now? if not i can do a table for all the iterations

    for you multiplication table just put the two numbers in the frist and third colum and mutilply them together for your fifth colum

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    I'm gonna play with this but please do a table for all it will help me a lot. this is just a child form for a big project that I'd rather be done already. Thanks again!
    Twisted

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Ok how do I get my num1 to show in (column 1)colMultiplicand and num2 to show in (column 3) colMultiplier, then (column 5)colProduct = num1 * num2
    Twisted

  7. #7
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Do you know how to add items & subitems to a listview? You might want to look it up, try it, and post some code if you have problems.
    Basically, the first column is a listview item, and the other columns are subitems of that. They can be added with .Add, using the counter as an index.
    OK, here's an example. You can also add items & subitems at design time. There are other ways to do it.
    VB Code:
    1. Dim LvItem as New ListViewItem()   'this would be outside a loop
    2.  
    3. 'one way
    4. LvItem.Text = "Blah blah"  
    5. LvItem.SubItems.Add("yada yada")
    6. LvItem.Subitems.Add(strText)
    7. myListView.Items.Add(LvItem)
    8.  
    9. 'another way
    10. Set LvItem = myListView.Items.Add("Blah blah")
    11. LvItem.SubItems(1) = "yada yada"
    12. LvItem.SubItems(2) = strText
    13.  
    14. 'so
    15. Dim LvItem as New ListViewItem()
    16. For i = 1 to 12
    17.    For j = 1 to 12
    18.       Set LvItem = myListView.Items.Add(i)
    19.       LvItem.SubItems(1) = "x"
    20.       LvItem.SubItems(2) = j
    21.       LvItem.SubItems(3) = "="
    22.       LvItem.SubItems(1) = i * j
    23.    Next
    24. Next
    Last edited by salvelinus; Jun 22nd, 2004 at 11:47 AM.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Here is my code with errors under the ones in RED:
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim LvItem As New ListViewItem
    3.         Dim int1 As Integer
    4.         Dim int2 As Integer
    5.         For int1 = 1 To 12
    6.             For int2 = 1 To 12
    7.                 LvItem = lstTable.Items.Add("int1")
    8.                 LvItem.SubItems(1) = [COLOR=RED]"X"[/COLOR]
    9.                 LvItem.SubItems(2) = [COLOR=RED]int2[/COLOR]
    10.                 LvItem.SubItems(3) = [COLOR=RED]("=")[/COLOR]
    11.                 LvItem.SubItems(4) = [COLOR=RED](int1 * int2)[/COLOR]
    12.             Next
    13.         Next
    14.  
    15.     End Sub
    16. End Class

    Also when I execute the program my Column just fills with int1, not the actual value
    Last edited by twisted; Jun 22nd, 2004 at 12:42 PM.
    Twisted

  9. #9
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    You have int1 in quotes. It's an integer, not a string.
    Anyway, try this. Works for me. The other ways are illustrated in Mastering Visual Basic. Don't forget, you have to add the columns and set the listview to report view first.
    VB Code:
    1. For i = 1 To 12
    2.             For j = 1 To 12
    3.                 LvItem = myListView.Items.Add(i)
    4.                 LvItem.SubItems.Add("x")
    5.                 LvItem.SubItems.Add(j)
    6.                 LvItem.SubItems.Add("=")
    7.                 LvItem.SubItems.Add(i * j)
    8.             Next
    9.         Next

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Doesn't work for me?? Don't know why. Also if I don't put int1.ToString it will give me a blue line under it. Everything in RED is giving me a blue line under it??

    VB Code:
    1. For int1 = 1 To 12
    2.             For int2 = 1 To 12
    3.                 LvItem = lstTable.Items.Add(int1.ToString)
    4.                 LvItem.SubItems(1) = [COLOR=RED]"X" [/COLOR]
    5.                 LvItem.SubItems(2) = [COLOR=RED]int2[/COLOR]
    6.                 LvItem.SubItems(3) = [COLOR=RED]("=")[/COLOR]
    7.                 LvItem.SubItems(4) = [COLOR=RED](int1 * int2)[/COLOR]
    8.  
    9.             Next
    10.         Next
    Twisted

  11. #11
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Notice I'm not using the SubItems(index) method in the last example. Use SubItems.Add()
    You might have to cast it if you're using Option Strict, which you normally should. I tested the code real quick w/o it. In that case you'd have to calculate the product (i * j) and cast it to what you want.
    VB Code:
    1. Dim LvItem As New ListViewItem()
    2.         Dim i, j, ans As Integer
    3.         For i = 1 To 12
    4.             For j = 1 To 12
    5.                 LvItem = myListView.Items.Add(i.ToString)
    6.                 LvItem.SubItems.Add("x")
    7.                 LvItem.SubItems.Add(j.ToString)
    8.                 LvItem.SubItems.Add("=")
    9.                 ans = i * j
    10.                 LvItem.SubItems.Add(ans.ToString)
    11.             Next
    12.         Next
    Last edited by salvelinus; Jun 22nd, 2004 at 02:30 PM.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Overload resolution failed because no accessible 'Add' accepts this number of arguments. Errors in RED!

    VB Code:
    1. [COLOR=RED] LvItem.SubItems.Add[/COLOR] = "X"
    2. [COLOR=RED] LvItem.SubItems.Add[/COLOR] = int2
    3. [COLOR=RED] LvItem.SubItems.Add[/COLOR] = ("=")
    4. [COLOR=RED] LvItem.SubItems.Add[/COLOR]= (int1 * int2)
    Twisted

  13. #13
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    Originally posted by twisted
    Overload resolution failed because no accessible 'Add' accepts this number of arguments. Errors in RED!

    VB Code:
    1. [COLOR=RED] LvItem.SubItems.Add[/COLOR] = "X"
    2. [COLOR=RED] LvItem.SubItems.Add[/COLOR] = int2
    3. [COLOR=RED] LvItem.SubItems.Add[/COLOR] = ("=")
    4. [COLOR=RED] LvItem.SubItems.Add[/COLOR]= (int1 * int2)
    This is because you used
    VB Code:
    1. LvItem.SubItems.Add = "X"
    instead of
    VB Code:
    1. LvItem.SubItems.Add("X")

    'Add' is a method which accepts arguments and cannot be the target of an assignment.

  14. #14
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Look at the example. You're not doing it correctly.

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Is this better b/c the errors are still in the same places??

    VB Code:
    1. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim LvItem As New ListViewItem
    3.         Dim int1, int2, ans As Integer
    4.  
    5.         For int1 = 1 To 12
    6.             For int2 = 1 To 12
    7.                 LvItem = lstTable.Items.Add(int1.ToString)
    8.                 [COLOR=RED]LvItem.SubItems.Add[/COLOR] = ("X")
    9.                 [COLOR=RED]LvItem.SubItems.Add[/COLOR] = (int2.ToString)
    10.                 [COLOR=RED]LvItem.SubItems.Add[/COLOR] = ("=")
    11.                 ans = int1 * int2
    12.                 [COLOR=RED]LvItem.SubItems.Add[/COLOR] = (ans.ToString)
    13.  
    14.             Next
    15.         Next
    16.  
    17.     End Sub
    Twisted

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    The first column is turning out great and looping perfectly. Just can't get the other subitems to add.
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim LvItem As New ListViewItem
    3.         Dim int1, int2, ans As Integer
    4.  
    5.         For int1 = 1 To 12
    6.             For int2 = 1 To 12
    7. ' Works Perfect
    8.                 LvItem = lstTable.Items.Add(int1.ToString)
    9. ' Doesn't Add
    10.                 LvItem.SubItems.Add = ("X")
    11. ' Doesn't Add
    12.                 LvItem.SubItems.Add = (int2.ToString)
    13. ' Doesn't Add
    14.                 LvItem.SubItems.Add = ("=")
    15. ' Works Perfect
    16.                 ans = int1 * int2
    17. ' Doesn't Add
    18.                 LvItem.SubItems.Add = (ans.ToString)
    19.             Next
    20.         Next
    21.  
    22.     End Sub
    Twisted

  17. #17
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    VB Code:
    1. LvItem.SubItems.Add = ("X")  'WRONG!
    2.  
    3. LvItem.SubItems.Add("X")   'RIGHT!

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    HAHA! I don't listen very well do I? You said that above and I kept looking over it. Thanks man, it works perfect!
    Twisted

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width