NewB Question

  • Thread starter Thread starter whaletyr
  • Start date Start date
W

whaletyr

I need to add several (a variable number of ) labels in runtime to my form.

I am using VB2003 and i have searched for answers and i should change the
Index. I cannot find where or how to do it, and I want to know what is the
easiest way to add labels or textboxes to a form in runtime.

Any help hint and tips would be welcome
 
* "whaletyr said:
I need to add several (a variable number of ) labels in runtime to my form.

I am using VB2003 and i have searched for answers and i should change the
Index.
Index?

I cannot find where or how to do it, and I want to know what is the
easiest way to add labels or textboxes to a form in runtime.

\\\
Dim b As New Button()
With b
.Text = ...
...
End With
Me.Controls.Add(b)
..
..
..
Me.Controls.Remove(b)
///
 
I need to add several (a variable number of ) labels in runtime to my form.

Dim MyLabel As New Label
MyLabel.Text = "My Label"
Me.Controls.Add(MyLabel)

Hope this helps.

Blu
 
I did get the add controls command, but my problem is mainly that I need to
add a variable number of labels with not a fixed name

eg
I need 5 labels where 5 can be any number between 1 and 40 or so

I cant use dim l(5) as new label
with l
.Text = ...
...
End With
Me.Controls.Add(l(5))

So is there another easier way to do this?
 
* "whaletyr said:
I did get the add controls command, but my problem is mainly that I need to
add a variable number of labels with not a fixed name

eg
I need 5 labels where 5 can be any number between 1 and 40 or so

I cant use dim l(5) as new label
with l
.Text = ...
...
End With
Me.Controls.Add(l(5))

\\\
Dim albl(4) As Label
Dim i As Integer
For i = 0 to albl.Length - 1
albl(i) = New Label()
...
Me.Controls.Add(albl(i))
Next i
///
 
Whale.

Does this old sample of mine give you some idea/s
(This is for a window forms I have the same for a webform)

I hope this helps?

Cor

\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim start As Integer = 4
Dim top As Integer = 25
Dim i As Integer
Dim nowdate As DateTime = DateTime.Now
Dim mybutton(System.DateTime.DaysInMonth _
(nowdate.Year, nowdate.Month)) As Button
For i = 0 To System.DateTime.DaysInMonth _
(nowdate.Year, nowdate.Month) - 1
mybutton(i) = New Button
mybutton(i).TextAlign = ContentAlignment.MiddleCenter
mybutton(i).Width = 40
mybutton(i).Height = 20
mybutton(i).FlatStyle = FlatStyle.Flat
mybutton(i).BackColor = Drawing.Color.AntiqueWhite
mybutton(i).Location = New System.Drawing.Point(start, top)
mybutton(i).Text = (i + 1).ToString
mybutton(i).Cursor = Cursors.Hand
Me.Controls.Add(mybutton(i))
AddHandler mybutton(i).Click, AddressOf mybutton_Click
AddHandler mybutton(i).MouseHover, AddressOf mybutton_Hoover
AddHandler mybutton(i).MouseLeave, AddressOf mybutton_Leave
start = start + 40
If (i + 1) Mod 5 = 0 Then
top = top + 20
start = 4
End If
Next
End Sub
Private Sub mybutton_Click _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim thisbutton As Button = DirectCast(sender, Button)
MessageBox.Show("The day is: " & thisbutton.Text)
End Sub
Private Sub mybutton_Hoover _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim thisbutton As Button = DirectCast(sender, Button)
thisbutton.BackColor = Drawing.Color.AliceBlue
End Sub
Private Sub mybutton_Leave _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim thisbutton As Button = DirectCast(sender, Button)
thisbutton.BackColor = Drawing.Color.AntiqueWhite
End Sub
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top