|
-
Jun 22nd, 2004, 08:04 AM
#1
Thread Starter
Addicted Member
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
-
Jun 22nd, 2004, 09:03 AM
#2
Frenzied Member
What is the processing you want to accomplish? Filling the listview? Calculating the product? Something else?
-
Jun 22nd, 2004, 09:28 AM
#3
Thread Starter
Addicted Member
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:
Option Strict On
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents ListView1 As System.Windows.Forms.ListView
Friend WithEvents colMultiplicand As System.Windows.Forms.ColumnHeader
Friend WithEvents colX As System.Windows.Forms.ColumnHeader
Friend WithEvents colMultiplier As System.Windows.Forms.ColumnHeader
Friend WithEvents colEqual As System.Windows.Forms.ColumnHeader
Friend WithEvents colProduct As System.Windows.Forms.ColumnHeader
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ListView1 = New System.Windows.Forms.ListView
Me.colMultiplicand = New System.Windows.Forms.ColumnHeader
Me.colX = New System.Windows.Forms.ColumnHeader
Me.colMultiplier = New System.Windows.Forms.ColumnHeader
Me.colEqual = New System.Windows.Forms.ColumnHeader
Me.colProduct = New System.Windows.Forms.ColumnHeader
Me.SuspendLayout()
'
'ListView1
'
Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.colMultiplicand, Me.colX, Me.colMultiplier, Me.colEqual, Me.colProduct})
Me.ListView1.Location = New System.Drawing.Point(0, 0)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(304, 344)
Me.ListView1.TabIndex = 0
Me.ListView1.View = System.Windows.Forms.View.Details
'
'colMultiplicand
'
Me.colMultiplicand.Text = "Multiplicand"
Me.colMultiplicand.Width = 68
'
'colX
'
Me.colX.Text = "X"
Me.colX.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
Me.colX.Width = 19
'
'colMultiplier
'
Me.colMultiplier.Text = "Multiplier"
Me.colMultiplier.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
Me.colMultiplier.Width = 53
'
'colEqual
'
Me.colEqual.Text = "="
Me.colEqual.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
Me.colEqual.Width = 18
'
'colProduct
'
Me.colProduct.Text = "Product"
Me.colProduct.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
Me.colProduct.Width = 79
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(232, 326)
Me.Controls.Add(Me.ListView1)
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
End Sub
End Class
Last edited by twisted; Jun 22nd, 2004 at 09:32 AM.
Twisted
-
Jun 22nd, 2004, 10:00 AM
#4
Hyperactive Member
VB Code:
dim num1 as integer
dim num2 as integer
for num1 = 1 to 12
for num2 = 1 to 12
'the first time num1=1 num2=1
'secound time num1=1 num2=2
' num1=1 num2=3
'it will go lie that till num1=1 num2=12
' then num1=2 num2=1
num1=2 num2=2
num1=2 num2=3
next
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
-
Jun 22nd, 2004, 10:04 AM
#5
Thread Starter
Addicted Member
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!
-
Jun 22nd, 2004, 10:26 AM
#6
Thread Starter
Addicted Member
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
-
Jun 22nd, 2004, 11:18 AM
#7
Frenzied Member
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:
Dim LvItem as New ListViewItem() 'this would be outside a loop
'one way
LvItem.Text = "Blah blah"
LvItem.SubItems.Add("yada yada")
LvItem.Subitems.Add(strText)
myListView.Items.Add(LvItem)
'another way
Set LvItem = myListView.Items.Add("Blah blah")
LvItem.SubItems(1) = "yada yada"
LvItem.SubItems(2) = strText
'so
Dim LvItem as New ListViewItem()
For i = 1 to 12
For j = 1 to 12
Set LvItem = myListView.Items.Add(i)
LvItem.SubItems(1) = "x"
LvItem.SubItems(2) = j
LvItem.SubItems(3) = "="
LvItem.SubItems(1) = i * j
Next
Next
Last edited by salvelinus; Jun 22nd, 2004 at 11:47 AM.
-
Jun 22nd, 2004, 11:55 AM
#8
Thread Starter
Addicted Member
Here is my code with errors under the ones in RED:
VB Code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim LvItem As New ListViewItem
Dim int1 As Integer
Dim int2 As Integer
For int1 = 1 To 12
For int2 = 1 To 12
LvItem = lstTable.Items.Add("int1")
LvItem.SubItems(1) = [COLOR=RED]"X"[/COLOR]
LvItem.SubItems(2) = [COLOR=RED]int2[/COLOR]
LvItem.SubItems(3) = [COLOR=RED]("=")[/COLOR]
LvItem.SubItems(4) = [COLOR=RED](int1 * int2)[/COLOR]
Next
Next
End Sub
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
-
Jun 22nd, 2004, 12:55 PM
#9
Frenzied Member
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:
For i = 1 To 12
For j = 1 To 12
LvItem = myListView.Items.Add(i)
LvItem.SubItems.Add("x")
LvItem.SubItems.Add(j)
LvItem.SubItems.Add("=")
LvItem.SubItems.Add(i * j)
Next
Next
-
Jun 22nd, 2004, 02:11 PM
#10
Thread Starter
Addicted Member
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:
For int1 = 1 To 12
For int2 = 1 To 12
LvItem = lstTable.Items.Add(int1.ToString)
LvItem.SubItems(1) = [COLOR=RED]"X" [/COLOR]
LvItem.SubItems(2) = [COLOR=RED]int2[/COLOR]
LvItem.SubItems(3) = [COLOR=RED]("=")[/COLOR]
LvItem.SubItems(4) = [COLOR=RED](int1 * int2)[/COLOR]
Next
Next
-
Jun 22nd, 2004, 02:27 PM
#11
Frenzied Member
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:
Dim LvItem As New ListViewItem()
Dim i, j, ans As Integer
For i = 1 To 12
For j = 1 To 12
LvItem = myListView.Items.Add(i.ToString)
LvItem.SubItems.Add("x")
LvItem.SubItems.Add(j.ToString)
LvItem.SubItems.Add("=")
ans = i * j
LvItem.SubItems.Add(ans.ToString)
Next
Next
Last edited by salvelinus; Jun 22nd, 2004 at 02:30 PM.
-
Jun 22nd, 2004, 02:32 PM
#12
Thread Starter
Addicted Member
Overload resolution failed because no accessible 'Add' accepts this number of arguments. Errors in RED!
VB Code:
[COLOR=RED] LvItem.SubItems.Add[/COLOR] = "X"
[COLOR=RED] LvItem.SubItems.Add[/COLOR] = int2
[COLOR=RED] LvItem.SubItems.Add[/COLOR] = ("=")
[COLOR=RED] LvItem.SubItems.Add[/COLOR]= (int1 * int2)
-
Jun 22nd, 2004, 03:26 PM
#13
Addicted Member
Originally posted by twisted
Overload resolution failed because no accessible 'Add' accepts this number of arguments. Errors in RED!
VB Code:
[COLOR=RED] LvItem.SubItems.Add[/COLOR] = "X"
[COLOR=RED] LvItem.SubItems.Add[/COLOR] = int2
[COLOR=RED] LvItem.SubItems.Add[/COLOR] = ("=")
[COLOR=RED] LvItem.SubItems.Add[/COLOR]= (int1 * int2)
This is because you used
VB Code:
LvItem.SubItems.Add = "X"
instead of
'Add' is a method which accepts arguments and cannot be the target of an assignment.
-
Jun 22nd, 2004, 03:27 PM
#14
Frenzied Member
Look at the example. You're not doing it correctly.
-
Jun 23rd, 2004, 07:24 AM
#15
Thread Starter
Addicted Member
Is this better b/c the errors are still in the same places??
VB Code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim LvItem As New ListViewItem
Dim int1, int2, ans As Integer
For int1 = 1 To 12
For int2 = 1 To 12
LvItem = lstTable.Items.Add(int1.ToString)
[COLOR=RED]LvItem.SubItems.Add[/COLOR] = ("X")
[COLOR=RED]LvItem.SubItems.Add[/COLOR] = (int2.ToString)
[COLOR=RED]LvItem.SubItems.Add[/COLOR] = ("=")
ans = int1 * int2
[COLOR=RED]LvItem.SubItems.Add[/COLOR] = (ans.ToString)
Next
Next
End Sub
-
Jun 23rd, 2004, 07:49 AM
#16
Thread Starter
Addicted Member
The first column is turning out great and looping perfectly. Just can't get the other subitems to add.
VB Code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim LvItem As New ListViewItem
Dim int1, int2, ans As Integer
For int1 = 1 To 12
For int2 = 1 To 12
' Works Perfect
LvItem = lstTable.Items.Add(int1.ToString)
' Doesn't Add
LvItem.SubItems.Add = ("X")
' Doesn't Add
LvItem.SubItems.Add = (int2.ToString)
' Doesn't Add
LvItem.SubItems.Add = ("=")
' Works Perfect
ans = int1 * int2
' Doesn't Add
LvItem.SubItems.Add = (ans.ToString)
Next
Next
End Sub
-
Jun 23rd, 2004, 09:04 AM
#17
Frenzied Member
VB Code:
LvItem.SubItems.Add = ("X") 'WRONG!
LvItem.SubItems.Add("X") 'RIGHT!
-
Jun 23rd, 2004, 09:38 AM
#18
Thread Starter
Addicted Member
HAHA! I don't listen very well do I? You said that above and I kept looking over it. Thanks man, it works perfect!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|