3 Replies - 2671 Views - Last Post: 24 November 2009 - 12:45 PM Rate Topic: -----

#1 itistimmah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 19-November 09

Loop Through Labels to Output Text from Array

Posted 22 November 2009 - 11:47 PM

Ok... Here's my problem.

I'm trying to loop through Labels that are named Label1 through Label16 and output integers from Array into the Text property of the labels. I think everything works except I can't figure out how to assign MyLabel the value of the current Label # in the loop.

		Dim MyLabel As Label	  
		Dim z, i, j As Integer
		Dim Array(,) As Integer = {{2, 300, 450, 750, 1100}, _
								   {4, 400, 500, 800, 1000}, _
								   {6, 350, 450, 600, 800}, _
								   {12, 100, 180, 210, 280}}
				z = 1

		For i = 0 To 4

			For j = 0 To 3

				MyLabel = Label & z

				MyLabel.Text = Array(i, j)

				j += 1
				z += 1

			Next j

			i += 1

		Next i


This post has been edited by itistimmah: 23 November 2009 - 02:36 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Loop Through Labels to Output Text from Array

#2 ZRonZ  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 206
  • Joined: 09-January 09

Re: Loop Through Labels to Output Text from Array

Posted 23 November 2009 - 01:31 PM

I didn't have time to work on the text box names but I did notice that you had some problems with your looping. I find it easier if I can see what's going on with the loops so I created a windows form, added a button1 and 20 "MyLabel"s numbered 1 to 20. Your loop, if properly executed will return 20 values, not the 16. The first loop is 0 to 4 which is 5 time second loop 0 to 3 which is 4, Hence the 20 values.

The first problem I saw was that the i and j were backwards. The first number in the array is for the "row" and the second is for the "column"

Once the form is set up, use the following code:

Public Class Form1

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		'Dim MyLabel As Label
		Dim z, i, j As Integer
		Dim Array(,) As Integer = {{2, 300, 450, 750, 1100}, _
								   {4, 400, 500, 800, 1000}, _
								   {6, 350, 450, 600, 800}, _
								   {12, 100, 180, 210, 280}}
		z = 1

		For i = 0 To 3

			For j = 0 To 4

				Select Case z
					Case Is = 1
						MyLabel1.Text = Array(i, j)
					Case Is = 2
						MyLabel2.Text = Array(i, j)
					Case Is = 3
						MyLabel3.Text = Array(i, j)
					Case Is = 4
						MyLabel4.Text = Array(i, j)
					Case Is = 5
						MyLabel5.Text = Array(i, j)
					Case Is = 6
						MyLabel6.Text = Array(i, j)
					Case Is = 7
						MyLabel7.Text = Array(i, j)
					Case Is = 8
						MyLabel8.Text = Array(i, j)
					Case Is = 9
						MyLabel9.Text = Array(i, j)
					Case Is = 10
						MyLabel10.Text = Array(i, j)
					Case Is = 11
						MyLabel11.Text = Array(i, j)
					Case Is = 12
						MyLabel12.Text = Array(i, j)
					Case Is = 13
						MyLabel13.Text = Array(i, j)
					Case Is = 14
						MyLabel14.Text = Array(i, j)
					Case Is = 15
						MyLabel15.Text = Array(i, j)
					Case Is = 16
						MyLabel16.Text = Array(i, j)
					Case Is = 17
						MyLabel17.Text = Array(i, j)
					Case Is = 18
						MyLabel18.Text = Array(i, j)
					Case Is = 19
						MyLabel19.Text = Array(i, j)
					Case Is = 20
						MyLabel20.Text = Array(i, j)

				End Select

			Next j
		Next i
	End Sub
End Class


That will show how the data is coming out of the loops.

The first problem I ran into was that the i and j were backwards. The first number in the array is for the "row" and the second is for the "column"
The second problem was that you were incrementing the variables that were already incrementing themselves.

		   For j = 0 To 3

				MyLabel = Label & z

				MyLabel.Text = Array(i, j)

				j += 1
				z += 1

			Next j


When you use a For/Next it will start with whatever you set the first number at, in this case j= 0. Then you added 1 (j += 1) and then j will equal = 1. "Next j" adds 1 so after the first loop j = 2. You probably expected it to equal 1.

Now that the array is being set up properly, maybe you can figure out the label name issue. Sorry, I only had a few minutes and just ran out of time.
Was This Post Helpful? 1
  • +
  • -

#3 itistimmah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 19-November 09

Re: Loop Through Labels to Output Text from Array

Posted 23 November 2009 - 09:46 PM

Thanks for explaining the For/Next loop for me. I hadn't executed the code yet. I just finished the C++ segment of my class and have the =+ and ++ operators stuck in my head. I'll give it a good try and post what I come up with. Thanks
Was This Post Helpful? 0
  • +
  • -

#4 ZRonZ  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 206
  • Joined: 09-January 09

Re: Loop Through Labels to Output Text from Array

Posted 24 November 2009 - 12:45 PM

I talked to a better coder than I am and he said that you can't concateate the label names. However, you could put them all in a group box and then treat them as objects and loop through them as objects.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1