Creating Controls

tomshirvo

New member
Joined
May 25, 2008
Messages
3
Programming Experience
1-3
I am creating controls from code so for example

Dim tb As New TextBox()
tb.Location = New Point(50, 100)
tb.Size = New Size(100, 50)
tb.Name = "tb2"
Me.Controls.Add(tb)

Now with this l have named the tb to tb2

Now in the code how do I reference this. If l go like this

tb2.text = "hi"

it will now work because it is telling me that the control is not there.
 
Access it from the Controls collection by name, for example Me.Controls("tb2"). This return an object of type Control, use DirectCast statement if you need a more specific type.
 
What is DirectCast
It's a type casting function similar to CType, look them up in help documentation.
what about when l want to have a click event for a button or something
For dynamically created controls you have to use the AddHandler statement to add event handlers.
 
Back
Top