Print values dynamically on a form

  • Thread starter Thread starter Rick
  • Start date Start date
R

Rick

I have a form that needs to display values from an array on the form; how do
I achieve the following?

If item count in array is < 20 then print

Val1 Val2
Val3 Val4
Val5 Val6
and so on

If items in array is > 20 then print

Val1 Val2 Val3
Val4 Val5 Val6
Val7 Val8 Val9
and so on

The following code works fine and prints values in two columns butI need to
modified it to print in three columns if nItemsCount > 20

for i=1 to nItemsCount

If i Mod 2 Then
nX = nX * 25
nY = nY - 15
End If

myVal.Name = "myVal" & i
myVal.Text = sLabel
myVal.Location = New Point(nX, nY + 15)
Controls.Add(myTextBoxLabel)
Next
 
I have a form that needs to display values from an array on the form; how do
I achieve the following?

If item count in array is< 20 then print

Val1 Val2
Val3 Val4
Val5 Val6
and so on

If items in array is> 20 then print

Val1 Val2 Val3
Val4 Val5 Val6
Val7 Val8 Val9
and so on

The following code works fine and prints values in two columns butI need to
modified it to print in three columns if nItemsCount> 20

for i=1 to nItemsCount

If i Mod 2 Then
nX = nX * 25
nY = nY - 15
End If

myVal.Name = "myVal"& i
myVal.Text = sLabel
myVal.Location = New Point(nX, nY + 15)
Controls.Add(myTextBoxLabel)
Next

I don't see your code as working the way you believe it is. That said,
here is how I would do what you seem to want:

Dim nItemsCount As Integer = 22 ' or some other value...
Dim nx As Integer = 10
Dim ny As Integer = 10

Dim nc As Integer = 2
If (nItemsCount > 20) Then nc = 3

For i = 1 To nItemsCount
Dim myVal As Label = New Label()

myVal.Name = "myVal" & i
myVal.Text = myVal.Name
myVal.Location = New Point(nx, ny)

nx = nx + myVal.Width + 10

If (i Mod nc = 0) Then
nx = 10
ny = ny + 25
End If

Controls.Add(myVal)
Next
 
Thanks

Family Tree Mike said:
I don't see your code as working the way you believe it is. That said,
here is how I would do what you seem to want:

Dim nItemsCount As Integer = 22 ' or some other value...
Dim nx As Integer = 10
Dim ny As Integer = 10

Dim nc As Integer = 2
If (nItemsCount > 20) Then nc = 3

For i = 1 To nItemsCount
Dim myVal As Label = New Label()

myVal.Name = "myVal" & i
myVal.Text = myVal.Name
myVal.Location = New Point(nx, ny)

nx = nx + myVal.Width + 10

If (i Mod nc = 0) Then
nx = 10
ny = ny + 25
End If

Controls.Add(myVal)
Next
 
Back
Top