Adding new unknown control at runtime

  • Thread starter José Teixeira Junior
  • Start date
J

José Teixeira Junior

I need add one new unknown control at runtime and the only one information
that i have is one string with type of control.

Example:
c = "System.Windows.Forms.TextBox"

How i can create one textbox at runtime in my form1?
 
C

Claude Froment

-----Original Message-----
I need add one new unknown control at runtime and the only one information
that i have is one string with type of control.

Example:
c = "System.Windows.Forms.TextBox"

How i can create one textbox at runtime in my form1?


.
Hi José

Here is a code sample that will add a text box to your
current form.
Dim objText As TextBox = New TextBox 'Create text control
objText.Visible = True 'Make it visible
objText.Name = "myText"
objText.Location = New Point(10, 10) 'Set the x=10 and y
= 10
objText.Height = 20
objText.Width = 200
objText.Text = "Hello!"
Me.Controls.Add(objText) 'add text to current form
Just cut and past this code sample inside a button click
event and you should see a text box.
Good luck,
Claude
 
M

Morpheu

Hi Claude,

Thanks for you help, but, i don't know the type of my control in advance.
I'm sweeping a collection that contains more values of controls.

As result, i have:

For i = 0 to colControls.Count - 1
strControl = colControls(i).StringControl
Next

In this point, i need to create control starting from the string strControl.

Alerts:
strControl as string
colControls as my collection (can't modify your structure because it is a
external collection - other component that is not on my control)

Then, who can help me?


-----Original Message-----
I need add one new unknown control at runtime and the only one information
that i have is one string with type of control.

Example:
c = "System.Windows.Forms.TextBox"

How i can create one textbox at runtime in my form1?


.
Hi José

Here is a code sample that will add a text box to your
current form.
Dim objText As TextBox = New TextBox 'Create text control
objText.Visible = True 'Make it visible
objText.Name = "myText"
objText.Location = New Point(10, 10) 'Set the x=10 and y
= 10
objText.Height = 20
objText.Width = 200
objText.Text = "Hello!"
Me.Controls.Add(objText) 'add text to current form
Just cut and past this code sample inside a button click
event and you should see a text box.
Good luck,
Claude
 
F

Fergus Cooney

Hi Guys,

Just in case you're interested, I'll save you the trek to languages.vb.

The following will create a Control given a name such a "Button".

Public Function MakeControl (sTypeName As String) As Control
Dim sFormTypeName As String = GetType (Form).AssemblyQualifiedName
sTypeName = sFormTypeName.Replace ("Form,", sTypeName & ",")
Dim oType As Type = Type.GetType (sTypeName)
Return DirectCast (Activator.CreateInstance (oType), Control)
End Function

This was given in answer to a similar query a week ago. For an explanation
of how it works, see
http://tinyurl.com/r393

Regards,
Fergus
 
J

Jon Skeet [C# MVP]

Fergus Cooney said:
Just in case you're interested, I'll save you the trek to languages.vb.

The following will create a Control given a name such a "Button".

Public Function MakeControl (sTypeName As String) As Control
Dim sFormTypeName As String = GetType (Form).AssemblyQualifiedName
sTypeName = sFormTypeName.Replace ("Form,", sTypeName & ",")
Dim oType As Type = Type.GetType (sTypeName)
Return DirectCast (Activator.CreateInstance (oType), Control)
End Function

This was given in answer to a similar query a week ago. For an explanation
of how it works, see
http://tinyurl.com/r393

That looks complicated and slightly tricksy to me.

You can make the code simpler than that:

Dim formsAssembly as Assembly = GetType (Form).Assembly
Dim oType as Type = formsAssembly.GetType _
("System.Windows.Forms."& sTypeName)
Return DirectCase (Activator.CreateInstance (oType), Control)

There's no need to start replacing bits of the type name - just find
out which assembly the type should be in, and fetch it directly from
that.

I'd personally make the parameter the fully qualified type name
(without the assembly, but with the namespace), at which point the
middle line is just
Dim oType as Type = formsAssembly.GetType(sTypeName)
 
F

Fergus Cooney

Hi Jon,

That was produced by trial and error and MSDN. I know Types better than I
do Assembly.

Thank you. I have a bit more balance. ;-)

Regards,
Fergus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top