Click to See Complete Forum and Search --> : Adding Controls at Runtime


nolc
May 21st, 2002, 03:31 PM
Does anyone know how to add controls at runtime?

dim btn as new button '?

I want to add an array of user controls which I have created and I'm stumped!

Thanks....

Akim
May 21st, 2002, 03:55 PM
I haven't tried that, but... here is the link:
http://support.microsoft.com/directory/article.asp?ID=kb;en-us;Q308433&SD=MSDN

nolc
May 21st, 2002, 04:14 PM
Looks good Akim! Thanks! ;)

Iouri
May 21st, 2002, 07:12 PM
Dim Form2 as New Form()
Dim btnCancel as New Button()

'set button properties
btnCancel.text = "Cancel"
btnCancel.Location = New Point(110,100)

'add new object to control collection
Form2.Controls.Add(btnCancel)

'show form as DialogBox
Form2.ShowDialog()

another example------------------------------------------

VB6 (This code adds a TextBox to the form and makes it visible.)
Dim myControl As Control
Set myControl = Me.Controls.Add("VB.TextBox", "myControl")
myControl.Visible = True
'remove control
Me.Controls.Remove "myControl"

VB.NET
Dim c As Control
c = New TextBox()
c.Name = "myControl"
Me.Controls.Add(c)

In Windows Forms, controls are indexed by number, not by name. To remove a control by name, you have
to iterate through the controls collection, find the control, and remove it. The following code shows how
to remove the newly added control by name:

Dim c As Control
For Each c In Me.Controls
If c.Name = "myControl" Then
Me.Controls.Remove(c)
Exit For
End If
Next

Akim
May 21st, 2002, 07:57 PM
Here is THE MAN :)
Thanks Iouri.