473,320 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Adding new unknown control at runtime

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?
Nov 22 '05 #1
7 2400
Take a look at System.Reflection namespace.
It should get you started.

"José Teixeira Junior" <te*************@stinfo.com.br> wrote in message
news:Op**************@TK2MSFTNGP11.phx.gbl...
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?

Nov 22 '05 #2
-----Original Message-----
I need add one new unknown control at runtime and the only one informationthat 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

Nov 22 '05 #3
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?
"Claude Froment" <cl************@rogers.com> escreveu na mensagem
news:14****************************@phx.gbl...
-----Original Message-----
I need add one new unknown control at runtime and the only one informationthat 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
Nov 22 '05 #4
Fergus help me in microsoft.public.dotnet.languages.vb.

Thanks everybody.
Morpheu

"Morpheu" <mo*****@hotmail.com> escreveu na mensagem
news:u4**************@TK2MSFTNGP12.phx.gbl...
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?
"Claude Froment" <cl************@rogers.com> escreveu na mensagem
news:14****************************@phx.gbl...
-----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

Nov 22 '05 #5
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

Nov 22 '05 #6
Fergus Cooney <fi******@tesco.net> wrote:
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)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #7
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
Nov 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: José Teixeira Junior | last post by:
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...
3
by: DJ Dev | last post by:
Hi All, I would like to add the <div> tag programatically from the code-behind of my aspx file. Basically, I would like to a table to this <div>. There are a number of tables which are created...
4
by: MattB | last post by:
Hello. I'm creating a page to edit a person's record (name address, etc) using a DataGrid. I want the fields that can be edited to be created at runtime based on a list of columns in web.config....
5
by: Lars-Erik Aabech | last post by:
Hi! Guess it's my day again.. Tried to deploy a test release of a new asp.net web today, and got a terrible error. The web is running swell on three development computers, but when it's copied...
22
by: José Teixeira Junior | last post by:
Hi, How i can add one new unknown control at runtime in my form1? Thaks.
6
by: Morpheu | last post by:
Hello, I have other problem with the unknown control added at runtime: When i use a mdiformchild, the control don't appear. In a normal form, it works. Somebody? Thank you in advance....
4
by: rushikesh.joshi | last post by:
Hi All, I have created my own WebControl and want to add it in my aspx page at runtime. it's compiling perfectly, but when i m going to execute, it gives me error of "Object reference not set...
0
by: sonic | last post by:
I am trying to dynamically load a validator and must be missing something elementary here. I extended TextBox control to add some functionality to it. One new feature it contains is IsRequired...
11
by: Pete Kane | last post by:
Hi All, does anyone know how to add TabPages of ones own classes at design time ? ideally when adding a new TabControl it would contain tab pages of my own classes, I know you can achieve this with...
7
by: John | last post by:
Hi Everyone, I'm having this extremely annoying problem with Internet Explorer 6, giving me an error message saying "unknown runtime error" whenever I try to alter the contents of a <divelement...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.