Is there a better way to Create Controls?

Jason_deBono

Member
Joined
Jan 30, 2008
Messages
5
Programming Experience
3-5
Hi,
Is there a better way to create an Object from the contents of a String Variable.
I would like to remove the Select Case statement, cause I have to do a case for every possible control type.:eek:
*** CODE eg. ***
StrControlType = *SOME CONTROL TYPE*
Select Case StrControlType.ToUpper
Case Is = "Button".ToUpper
Dim objButton As New Button With {.Location = New Point(50, 50)}
Me.Controls.Add(objButton)

Case Is = "TextBox".ToUpper
Dim objTextBox As New TextBox With {.Location = New Point(50, 50)}
Me.Controls.Add(objTextBox)

End Select
 
For classes in Forms assembly you can do reflection like this:
VB.NET:
Expand Collapse Copy
Dim t As Type = GetType(Form)
Dim o As Object = t.Assembly.CreateInstance(t.Namespace & ".Button")
With DirectCast(o, Control)
    .Location = New Point(50, 50)
End With
Controls.Add(o)
 
Last edited:
Back
Top