473,405 Members | 2,310 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,405 software developers and data experts.

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 4800
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.