Control Arrays in .NET

  • Thread starter Thread starter PBSnake
  • Start date Start date
P

PBSnake

Guest
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
 
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
 
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.

Visual Basic:
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
 
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
 
That's pretty much what you'll have to do. Control arrays are more work in .net at the moment, no doubt about that. On the other hand, they're far more flexible. Wait for designer support for them in a future version, I guess.
 
Well, thanks for the help and the quick response. And you are correct I have found allot more good, then bad with .net.


PBSnake
 
Back
Top