473,473 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to create a control instance dynamicly by text.

Can anyone tell me how to to create a control instance dynamicly.

Example:
When I have a string value with the value 'TextBox'. I want to create a
TextBox instance.
I know that i can do the folowing: TextBox tb = new TextBox();

Now i've created a method that translates the string value:

fucntion CreateControl(string controlTypeName)
{
switch (controlTypeName)
{
case "Label":
return new Label();
case "TextBox":
return new TextBox();
}
}
but there must be a simpler way more generic way to do this.

Regards,
Jelle van Baardewijk
Nov 15 '05 #1
2 4801
Hi,

You might take a look at Activator.CreateInstance method.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

"Jelle van Baardewijk" <jv*@databalk.nl> wrote in message
news:uj****************@TK2MSFTNGP12.phx.gbl...
Can anyone tell me how to to create a control instance dynamicly.

Example:
When I have a string value with the value 'TextBox'. I want to create a
TextBox instance.
I know that i can do the folowing: TextBox tb = new TextBox();

Now i've created a method that translates the string value:

fucntion CreateControl(string controlTypeName)
{
switch (controlTypeName)
{
case "Label":
return new Label();
case "TextBox":
return new TextBox();
}
}
but there must be a simpler way more generic way to do this.

Regards,
Jelle van Baardewijk

Nov 15 '05 #2
You can use this code:

Assembly assem = Assembly.GetAssembly(typeof(Form));
Type controlType = assem.GetType(controlType);
object obj = Activator.CreateInstance(controlType);
Control tb = (Control)obj;
this.Controls.Add(tb);

controlType is a string which can be System.Windows.Forms.TextBox,
ComboBox....

Good Luck
"Jelle van Baardewijk" <jv*@databalk.nl> wrote in message
news:uj****************@TK2MSFTNGP12.phx.gbl...
Can anyone tell me how to to create a control instance dynamicly.

Example:
When I have a string value with the value 'TextBox'. I want to create a
TextBox instance.
I know that i can do the folowing: TextBox tb = new TextBox();

Now i've created a method that translates the string value:

fucntion CreateControl(string controlTypeName)
{
switch (controlTypeName)
{
case "Label":
return new Label();
case "TextBox":
return new TextBox();
}
}
but there must be a simpler way more generic way to do this.

Regards,
Jelle van Baardewijk

Nov 15 '05 #3

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

Similar topics

0
by: Michel | last post by:
On an empty ASP.NET page, a placeholder is added. On this page, a literal is added dynamicly, through the placeholder. On the same page, a custom control is dragged. The same custom control is...
1
by: paula | last post by:
I've got a problem with asp.net i am trying to make a menu control. and have searched the web for serveral controls but they don't work correctly. I am pretty new to asp.net building. What am i...
6
by: SamIAm | last post by:
Hi am creating a email application that needs to mail out a very large amount of emails. I have created a multithreaded c# application that using message queuing. I have created a threadpool of 5...
0
by: Patrick | last post by:
I'm working on a contact management application, and need a hand with one aspect... Here's what I want to create: ------------------------------------ A form split into two parts. There is a...
0
by: Jeti [work] | last post by:
I have tab control with several tab pages. Each tab page contains a few textboxes filled dynamicly from the code. Everything goes fine, but every textbox that is located on a tab page that has hot...
1
by: Kristoffer Arfvidson | last post by:
Hi! How can I access a label that I put inside a headertemplate inside a repeatercontrol from my codebehind? Or, can I dynamicly add the content of headertemplate from codebehind? Because I...
11
by: Özden Irmak | last post by:
Hello, In my VB.Net application I create a new instance of my form like : NewForm = new MyNewForm() But with this line, this new form is automatically shown. I want it to be hidden at the...
14
by: Jim Burns | last post by:
I just started with vb.net and I don't understand why there are no control arrays. what replaced them. I was trying to do drag and drop(What happened with that) and it looked like with no control...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.