Creating a control programmatically

I tried to create several LABELS (an array) programmatically but the controls didn’t appear in the window. What am I doing wrong?

Just an example below:

[i]Dim lbHeader() As Label
Dim sHeader As String = “Header1, Header2, Header3, Header4, Header5, Header6”

Dim h As Integer = 0
For h = 0 to Colunas.Ubound
lbHeader.Append(New Label)
lbHeader(h).Text = “Teste”
lbHeader(h).Top = self.top + 44
lbHeader(h).Left = self.Left+ (53 + (h*30))
lbHeader(h).Width = 100
lbHeader(h).Height = 20
lbHeader(h).Visible = True
lbHeader(h).Enabled = True
lbHeader(h).TextSize = 12
lbHeader(h).TextAlign = Label.AlignLeft
lbHeader(h).TextColor = RGB(255,255,255)
Next[/i]

You can’t create new controls from nothing. You need to drag 1 instance of a label to your window. Give it a name. Then you can

dim m As new label1 m.text = "Some words" //ETC
You do not use an element number as you create the controls. You will need to keep track of the total in code. You can, of course, use a loop to create as many controls as you like.

The first control must be part of a control set, otherwise it won’t work.

I draged a Label Control to the window1 (Label1) then I turned it into a Control Set, then I did this code below. But still not appearing.

[i] Dim lbHeader() As Label

Dim h As Integer = 0
For h = 0 to Colunas.Ubound
lbHeader.Append(New Label1)
lbHeader(h).Text = “Teste”
lbHeader(h).Top = self.top + 44
lbHeader(h).Left = self.Left+ (53 + (h*30))
lbHeader(h).Width = 100
lbHeader(h).Height = 20
lbHeader(h).Visible = True
lbHeader(h).Enabled = True
lbHeader(h).TextSize = 12
lbHeader(h).TextAlign = Label.AlignLeft
lbHeader(h).TextColor = RGB(255,255,255)
Next[/i]

Why don’t you start by the beginning and create one control, instead of using a for next.

See http://developer.xojo.com/new

Simple, simple. Complex will come when it works.

Thank a lot Michel! I got it… I thought I could use like an array. But I tried simple and it worked. Let me see if I can handle with it using “index” to track it.

Don’t use self.top and self.left to set the control position. That could be pushing the controls out of the visible window area.

Remember, control sets are not really arrays, so they don’t function exactly the same. Once created, you can refer to an individual control as

MySetName(elementNum).text

But other array operations won’t work.

There is another possible way : use an array to reference the members :

myLabels() as Label1
myLabels.Append(new Label1)

for i as integer = 0 to 10 myLabels.append(new Label1) myLabels(mylabels.Ubound).top = i * 22 next

Where it becomes even more interesting, is with a method to remove a member :

[code]Public Sub removeLabel(LabelIndex as Integer)
if LabelIndex > myLabels.Ubound then return

Label1(myLabels(labelIndex)).close
myLabels.remove(labelIndex)

for i as integer = myLabels.Ubound downto 0
Label1(myLabels(i)).index = i
next

End Sub
[/code]

That way you actually have an array of MyLabel which remains consecutive.