Jump to content
Xtreme .Net Talk

Control Arrays in .NET


Recommended Posts

Posted

Ok, In Vb6 I could create a module that would fill a array of labels on a form. But I am having troubles doing this in VB.Net. I want to beable to change the text on array of Labels on one of my forms. Any suggestions or help?

 

 

Thanks PBSnake

Posted

Here is an example from VB6, and is pulled from a Module. With this I can turn on and off the labels and change the text. I would like to do the same thing in VB.Net.

 

 

Case "Straight"

With frmMain

.frmunits.Caption = "Straight Burn"

.lblBurnType.Caption = "SEQ STR"

For i = 0 To 4

.lblBurnDescription(i).Visible = True

.txtBurnData(i).Visible = True

Next i

For i = 5 To 9

.lblBurnDescription(i).Visible = False

.txtBurnData(i).Visible = False

Next i

.lblBurnDescription(0).Caption = "Hello"

.lblBurnDescription(1).Caption = "Tomorrow"

.lblBurnDescription(2).Caption = "Ocean"

.lblBurnDescription(3).Caption = "Power"

.lblBurnDescription(4).Caption = "Sky"

 

End With

  • *Gurus*
Posted

The .NET Windows Forms Designers do not support control arrays at this time, but that doesn't mean you can't implement them, and with more flexibility than VB6 too. You just have to declare your array in code, and load and display them in code too.

 

Dim lblDescription(5) As Label

Sub InitLabels()
 Dim intCounter As Integer

 For intCounter = 0 To 5
   lblDescription(intCounter) = New Label()
   Controls.Add(lblDescription(intCounter))
   lblDescription(intCounter).Location = New Point(100, intCounter * 20)
   lblDescription(intCounter).Text = "Label " & intCounter.ToString()
 Next
End Sub

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

This almost seems like more work. With the array I was able to use for next loops to turn on and off the labels. and I could also change the text. Now I have to add this extra code to set up the way I want the label to look. Like Font, color, size, location etc. Am I missing something or is there any other way to do this? I also used it allot with text boxes too. Then I would have to set up background colors, border types etc.

 

And these lables or Text Boxes change all the time. I tried this and found that I couldn't just put something new on screen. Below is a simple test I made using the above code. I just wanted to have 2 buttons that would change the text in the boxes. And what If I want to place them on a frame or a tab strip?

 

 

 

 

Dim lblDescription(5) As Label

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

InitLabels("john")

 

End Sub

Sub InitLabels(ByVal test As String)

Dim intCounter As Integer

ReDim lblDescription(5)

For intCounter = 0 To 5

lblDescription(intCounter) = New Label()

Controls.Add(lblDescription(intCounter))

lblDescription(intCounter).Location = New Point(133 + (intCounter * 113), 39)

lblDescription(intCounter).Text = test & intCounter.ToString()

Next

End Sub

 

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

InitLabels("Paul")

End Sub

 

 

Thanks PBSnake

Posted

Well, thanks for the help and the quick response. And you are correct I have found allot more good, then bad with .net.

 

 

PBSnake

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...