dynamically creating lables

anhabib

Newcomer
Joined
Nov 1, 2004
Messages
3
Hi i want to make labels dynamically. The method i designed is below
now i want to give it a call like this
labelarea(312, 344, 64, 40, " text here ", label1)
for doing this i have to also write this line of code
Dim label1 As New Label()

which mean i have to know exactly the number of labels i want that is the problem i dont know the number is there any solution or can i make any are of variables with type label

any idea people

Public Sub labelarea(ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer, ByVal textValue As String, ByVal labels As Label)
labels.Location = New System.Drawing.Point(x, y)
labels.Name = "Label1"
labels.Size = New System.Drawing.Size(width, height)
labels.BorderStyle = BorderStyle.FixedSingle
labels.Text = textValue
labels.TextAlign = ContentAlignment.MiddleCenter
Me.Controls.Add(labels)
End Sub
 
you can declare the label before you actualy call the New...


Dim label1 as Label
labelarea(312, 344, 64, 40, " text here ", label1)



Public Sub labelarea(ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer, ByVal textValue As String, ByVal labels As Label)
labels = New Label
labels.Location = New System.Drawing.Point(x, y)
labels.Name = "Label1"
labels.Size = New System.Drawing.Size(width, height)
labels.BorderStyle = BorderStyle.FixedSingle
labels.Text = textValue
labels.TextAlign = ContentAlignment.MiddleCenter
Me.Controls.Add(labels)
End Sub
 
use factory patterns. factory the size, location and the actual label:
Code:
Public function DetermineLabelSize(ByVal lbl as Label) as Size
  Dim sz as Size = new Size(0,0)
 
' if needed use the label parameter to determine the size, 
' such as the length of the label text.
 
  return sz
end function
 
Public function DetermineLabelLocation(ByVal lbl as Label) as Point
  Dim pt as Point = new Point(0,0)
 
' if needed use the label parameter to determine the position, 
' such as the length of the label text.
 
  return pt
end function
 
Public Function  CreateLabel(ByVal textValue As String) as Label
'instance a Label
  dim lbl as label = new Label()
 
'Dont worry about the name, if you reference the 
'control by name, you are doing things wrong
 
'Setup the display:
  lbl.BorderStyle = BorderStyle.FixedSingle
  lbl.Text = textValue
  lbl.TextAlign = ContentAlignment.MiddleCenter

'determine the next location
  lbl.Location = DetermineLabelLocation(lbl)

'determine the next Size
  lbl.Size = DetermineLabelSize(lbl) 
 
'return the Label
  return lbl
end function
 
Function LabelsFromStringArray(byval s() as String)
  Dim i as integer
  for i = LBound(s) to UBound(s)
	Me.Controls.Add(CreateLabel(s(i)))
  next i
end function
 
Back
Top