6 Replies - 749 Views - Last Post: 21 July 2008 - 08:44 AM Rate Topic: -----

#1 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

For loops in VB?

Posted 20 July 2008 - 08:14 PM

Someone else had a question in this forum today that included this code. I was wondering if they could use a for loop in VB to simplify it?

Would it work in VB if they change this:
		Label1.Text = String.Empty  
		Label2.Text = String.Empty  
		Label3.Text = String.Empty  
		Label4.Text = String.Empty  
		Label5.Text = String.Empty  
		Label6.Text = String.Empty  
		Label7.Text = String.Empty  
		Label8.Text = String.Empty  
		Label9.Text = String.Empty  
		Label10.Text = String.Empty  
		Label11.Text = String.Empty  
		Label12.Text = String.Empty  
		Label13.Text = String.Empty  
		Label14.Text = String.Empty  
		Label15.Text = String.Empty  
		Label16.Text = String.Empty  
  
		Label1.Enabled = True  
		Label2.Enabled = True  
		Label3.Enabled = True  
		Label4.Enabled = True  
		Label5.Enabled = True  
		Label6.Enabled = True  
		Label7.Enabled = True  
		Label8.Enabled = True  
		Label9.Enabled = True  
		Label10.Enabled = True  
		Label11.Enabled = True  
		Label12.Enabled = True  
		Label13.Enabled = True  
		Label14.Enabled = True  
		Label15.Enabled = True  
		Label16.Enabled = True  
  
		Label1.BackColor = Color.Silver  
		Label2.BackColor = Color.Silver  
		Label3.BackColor = Color.Silver  
		Label4.BackColor = Color.Silver  
		Label5.BackColor = Color.Silver  
		Label6.BackColor = Color.Silver  
		Label7.BackColor = Color.Silver  
		Label8.BackColor = Color.Silver  
		Label9.BackColor = Color.Silver  
		Label10.BackColor = Color.Silver  
		Label11.BackColor = Color.Silver  
		Label12.BackColor = Color.Silver  
		Label13.BackColor = Color.Silver  
		Label14.BackColor = Color.Silver  
		Label15.BackColor = Color.Silver  
		Label16.BackColor = Color.Silver  




To something like this:

for (int x = 1; x <=16; x++)
		Label[x].Text = String.Empty  

		Label[x].Enabled = True  
				
		Label[x].BackColor = Color.Silver  



If what I have done is incorrect, can anyone show me the correct way to simplify it? Thanks ;)

btw I think I will reinstall my VB compiler (from an installation cd in my VB book) so I won't have to ask these easy questions here anymore.

Is This A Good Question/Topic? 0
  • +

Replies To: For loops in VB?

#2 DeCompile  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 301
  • Joined: 20-July 08

Re: For loops in VB?

Posted 20 July 2008 - 08:55 PM

If only you could switch and change code from C++ to other languages.

Life would be so much simplier yet so much more difficult :)

I believe what you're looking for is something like a control array ?

I am led to believe that VB.net does not contain this control array anymore.

Don't quote me on anything.. I'm usually wrong :D
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: For loops in VB?

Posted 20 July 2008 - 10:12 PM

DeCompile is on the right track, VB.NET doesn't have control arrays as it did in version 6.0. But you are not too far off either Olive. What you have written is not exactly a translation for what they are trying to do but a for loop is a good solution to simplify this.

What they need to do is create an array of labels. In VB.NET there is a Label object so we need to create an array with them like so...

' Create an array of Labels
Dim arLabels(9) As Label

' Create a new label and place it in each array subscript
' Then set the properties of that Label
' At the end we have an array of 10 Labels where we can place them on a form
' at any location we like.
For i As Integer = 0 To 9
     arLabels(i) = New Label()
     arLabels(i).Text = String.Empty
     arLabels(i).Enabled = True
     arLabels(i).BackColor = Color.Silver
Next



Here we create an array of 10 labels all set with some values and ready to be placed. We can set their position dynamically in the loop or access each one one by one and tell it where to be put on the form.

Hope this makes sense Olive. Enjoy!

"At DIC we be label array making code ninjas... we also make cookies at xmas time!" :snap:
Was This Post Helpful? 1

#5 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: For loops in VB?

Posted 20 July 2008 - 10:45 PM

View PostMartyr2, on 21 Jul, 2008 - 12:12 AM, said:

Here we create an array of 10 labels all set with some values and ready to be placed. We can set their position dynamically in the loop or access each one one by one and tell it where to be put on the form.

Hope this makes sense Olive. Enjoy!


Thank you. Yes, it makes sense. :^:
I forgot about dim as...
I'm too used to C++ and C#.

Next question, how do you set their position dynamically in the loop, and how do you access each one one by one and tell it where to be put on the form?
Was This Post Helpful? 0
  • +
  • -

#6 Damage  Icon User is offline

  • Lord of Schwing
  • member icon

Reputation: 28
  • View blog
  • Posts: 1,090
  • Joined: 05-June 08

Re: For loops in VB?

Posted 21 July 2008 - 12:16 AM

digging Martyr's ninja quotes. reminds me of "Ask a Ninja" :P
Was This Post Helpful? 0
  • +
  • -

#7 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: For loops in VB?

Posted 21 July 2008 - 03:55 AM

Quote

how do you set their position dynamically in the loop

you can use something like this:
' Assuming you are inside the loop
arLabels(i).Location = New Point(< X coordinate >, < Y coordinate >)


Quote

how do you access each one one by one and tell it where to be put on the form?

Create an Array of Point Objects which contain the coordinates for each label.
You can then assign them their respective positions like this:
' Again, assuming you're inside the loop
' Name of array of Point Objects = LabelLocationArr
arLabels(i).Location = LabelLocationArr(i)

Was This Post Helpful? 1
  • +
  • -

#8 WayneSpangler  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 106
  • Joined: 22-March 08

Re: For loops in VB?

Posted 21 July 2008 - 08:44 AM

Control arrays are not exacally dead. Try this:
Public Class Form1
	Dim position As New Point(10, 10)
	Dim siz As New Size(75, 25)
	Dim xDist As Integer = 85
	Dim yDist As Integer = 35

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Me.Show()
		For x As Integer = 0 To 30
			Dim lab As New Label
			lab.Size = siz
			lab.Location = position
			lab.Visible = True
			lab.Name = "Lab" & x
			lab.Text = lab.Name.ToString
			lab.BorderStyle = BorderStyle.FixedSingle
			AddHandler lab.Click, AddressOf Label_Click
			Me.Controls.Add(lab)
			position.X += xDist
			If position.X + siz.Width >= Me.ClientRectangle.Width Then
				position.X = 10
				position.Y += yDist
			End If
		Next
	End Sub

	Private Sub Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
		Dim lab As Label = sender
		MessageBox.Show(lab.Name.ToString & " was clicked!")
	End Sub
End Class

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1