How can I make an array of Label ?

  • Thread starter Thread starter Lighting_dragon
  • Start date Start date
L

Lighting_dragon

I try to make an array of label as this.

Dim ArrLabel(10) as System.Windows.Forms.Label
For i =0 to 10
ArrLabel(i) = New System.Windows.Forms.Label
ArrLabel(i).Left=25
ArrLabel(i).Top=25*i
ArrLabel(i).Name="Label" & i
ArrLabel(i).Text="Test" & i
ArrLabel(i).Show()

Next

But it didn't show on the form. Can you help me plz.
 
Lighting_dragon said:
I try to make an array of label as this.

Dim ArrLabel(10) as System.Windows.Forms.Label
For i =0 to 10
ArrLabel(i) = New System.Windows.Forms.Label
ArrLabel(i).Left=25
ArrLabel(i).Top=25*i
ArrLabel(i).Name="Label" & i
ArrLabel(i).Text="Test" & i
ArrLabel(i).Show()
Next

But it didn't show on the form. Can you help me plz.

arrLabel(i).Visible = True ???

Also, doesn't .Net dim starting from 0, meaning Dim ArrLabel(10) = 0 through
9... ArrLabel(10) would error?
 
* (e-mail address removed)-spam.invalid (Lighting_dragon) scripsit:
 
add the control useing me.controls.add(arrLabel(i)) and make sure you make
it visible
arrLable(i).visible = true
don't use .Show


Lighting_dragon said:
I try to make an array of label as this.

Dim ArrLabel(10) as System.Windows.Forms.Label
For i =0 to 10
ArrLabel(i) = New System.Windows.Forms.Label
ArrLabel(i).Left=25
ArrLabel(i).Top=25*i
ArrLabel(i).Name="Label" & i
ArrLabel(i).Text="Test" & i
ArrLabel(i).Show()

Next

But it didn't show on the form. Can you help me plz.
 
add the control useing me.controls.add(arrLabel(i)) and make sure you make
it visible
arrLable(i).visible = true
don't use .Show

Thats interesting, Ive never struck that before. Whats the difference (other
than method and property) between the 2 uses?

Thanks
Richard
 
-----Original Message-----
I try to make an array of label as this.

Dim ArrLabel(10) as System.Windows.Forms.Label
For i =0 to 10
ArrLabel(i) = New System.Windows.Forms.Label
ArrLabel(i).Left=25
ArrLabel(i).Top=25*i
ArrLabel(i).Name="Label" & i
ArrLabel(i).Text="Test" & i
ArrLabel(i).Show()

Next
But it didn't show on the form. Can you help me plz.
ArrLabel(i).Text="Test" & i
Me.Controls.Add(ArrLabel(i))

You don't need the Show Statement
.............
 
Grahammer,
Also, doesn't .Net dim starting from 0, meaning Dim ArrLabel(10) = 0 through
9... ArrLabel(10) would error?
Welcome to the VB.NET "off by one bug"! :-)

..NET arrays do start with 0! VB.NET lists the upper bound, not number of
elements in array definitions. So

Has elements 0 to 10!

Hope this helps
Jay
 
Thanks for all your help.
I have made a mistake with the array(10), only form 0 -->9.
Then I have add the Method Me.Controls.Add(arrlabel(x))

Now it works well.

But I have a problem. How can I write a Sub to control the Event
Label.OnClick of all the Labels in Array.
 
Lighting_dragon,
You can use AddHandler & RemoveHandler to add a sub as an event handler to
each element.

Something Like:

For i =0 to 10
ArrLabel(i) = New System.Windows.Forms.Label
ArrLabel(i).Left=25
ArrLabel(i).Top=25*i
ArrLabel(i).Name="Label" & i
ArrLabel(i).Text="Test" & i
ArrLabel(i).Show()
AddHandler ArrLabel(i).Click, Address Label_Click
Next

Public Sub Label_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub

Hope this helps
Jay

Lighting_dragon said:
Thanks for all your help.
I have made a mistake with the array(10), only form 0 -->9.
Then I have add the Method Me.Controls.Add(arrlabel(x))

Now it works well.

But I have a problem. How can I write a Sub to control the Event
Label.OnClick of all the Labels in Array.
 
I also have a need for this type of functionality, but on Web Forms

I have items that are variable in number that I wish to display on a web
form. And I would like to be able to specify or access the contents of
item(x) by referenced the x elements of that array/collection.

I have tried something like the following on the form load event but nothing
ever shows on the form.

Dim ArrLabel(10) As WebControls.Label
For x = 1 To 10
ArrLabel(x) = New WebControls.Label
ArrLabel(x).Visible = True
ArrLabel(x).Text = "Label "
Page.Controls.Add(ArrLabel(x))
Next


Any help or guidance is appreciated.

Fred
 
How would I do this with a Web form in a webapplication?


Jay B. Harlow said:
Lighting_dragon,
You can use AddHandler & RemoveHandler to add a sub as an event handler to
each element.

Something Like:

For i =0 to 10
ArrLabel(i) = New System.Windows.Forms.Label
ArrLabel(i).Left=25
ArrLabel(i).Top=25*i
ArrLabel(i).Name="Label" & i
ArrLabel(i).Text="Test" & i
ArrLabel(i).Show()
AddHandler ArrLabel(i).Click, Address Label_Click
Next

Public Sub Label_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub

Hope this helps
Jay
 
Hi Fred,

I think this sample does it all for you, this are buttons, however the
difference between a weblabel and a webbutton is very slight (it has
autopostback).

I hope this helps?

Cor

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim mybutton(31) As Button
Dim i As Integer
For i = 0 To New Date().DaysInMonth _
(New Date().Year, New Date().Month) - 1
mybutton(i) = New Button
mybutton(i).BackColor = Drawing.Color.White
mybutton(i).Text = (i + 1).ToString
mybutton(i).Width = New Unit(30)
Me.Panel1.Controls.Add(mybutton(i))
AddHandler mybutton(i).Click, AddressOf mybutton_Click
If (i + 1) Mod 5 = 0 Then
Me.Panel1.Controls.Add(New LiteralControl("<BR>"))
End If
Next
End Sub
Private Sub mybutton_Click _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim mylabel As New Label
Me.Panel1.Controls.Add(New LiteralControl("<BR><BR>"))
Me.Panel1.Controls.Add(mylabel)
mylabel.Text = "The day is: " & DirectCast(sender, Button).Text
End Sub
 
Hi Fred,

I forgot this.

Better is to make a new question next time, now it seems an answer to a
question to the OP (origianal poster).

Cor
 
Back
Top