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

Home Posts Topics Members FAQ

Create controls dynamically

Hi,

I am writing a GUI application. It will have a number of user defined
controls( I plan to use/create a user defined control that will output alarm
states that the user can also select and will open a dialog to give more
detailed information) .

The number of controls on the main GUI will vary from client site to client
site. I would therefore like to populate the dialog with these controls
dynamically.

e.g the number of controls to be put on the dialog comes from a
configuration file or the database.

I'd appreciate any help on how i put the user defined controls on the form
dynamically and how to manipulate the controls such as catching the click
event on them etc if they havent been put on the form at design time,

Thanks In Advance,
Macca
Nov 17 '05 #1
2 2517
macca,

If you know the type of the control, you can easily create an instance
of it through the static CreateInstance method on the Activator class.
Additionally, since most classes that derive from Control have parameterless
constructors (they should, at least), you don't need to pass anything.

If you have custom controls which require parameters to be called to the
constructor, then you will have to call the overload of CreateInstance which
will take those parameters.

Once you create the control, you can cast the return value to Control,
which will allow you easily modify the common properties (size, location,
etc, etc) and access the typical events (click, double click, etc, etc).

Again, if you have specific events you want to hook up to, you will have
to cast to an instance of the appropriate control, or use reflection to
connect the event. Of course, if you have a bunch of custom controls but
the specific functionality they expose is consistent for each of your custom
controls, an interface might be in order (or a base class they all derive
from) which you can cast to, to make it easier.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"macca" <as****@dsl.pipex.com> wrote in message
news:dM********************@pipex.net...
Hi,

I am writing a GUI application. It will have a number of user defined
controls( I plan to use/create a user defined control that will output
alarm states that the user can also select and will open a dialog to give
more detailed information) .

The number of controls on the main GUI will vary from client site to
client site. I would therefore like to populate the dialog with these
controls dynamically.

e.g the number of controls to be put on the dialog comes from a
configuration file or the database.

I'd appreciate any help on how i put the user defined controls on the form
dynamically and how to manipulate the controls such as catching the click
event on them etc if they havent been put on the form at design time,

Thanks In Advance,
Macca

Nov 17 '05 #2
If you look at Designer generated form section in your code, you'll see how
Visual Studio generates the necessary code to add the controls. You can just
copy that code as a base for creating your dynamic code. For example, here's
slightly modified and combined code that VS generated for textBox1, which
you can use to add additional texboxes at run time:

System.Windows.Forms.TextBox textBox1 = new System.Windows.Forms.TextBox();
textBox1.Location = new System.Drawing.Point(0, 0);
textBox1.Name = "textBox1";
textBox1.TabIndex = 1;
textBox1.Text = "Some default text";
textBox1.TextChanged += new
System.EventHandler(this.anyTextBox_TextChanged);
Controls.Add(textBox1);

Note that all of the textboxes will have the same event handler. To
identifiy the calling control in that handler, use the sender argument:

private void anyTextBox_TextChanged(object sender, System.EventArgs e)
{
MessageBox.Show(((TextBox)sender).Text);
}

Also, look into SuspendLayout() and ResumeLayout() methods.

Andrej

"macca" <as****@dsl.pipex.com> wrote in message
news:dM********************@pipex.net...
Hi,

I am writing a GUI application. It will have a number of user defined
controls( I plan to use/create a user defined control that will output
alarm states that the user can also select and will open a dialog to give
more detailed information) .

The number of controls on the main GUI will vary from client site to
client site. I would therefore like to populate the dialog with these
controls dynamically.

e.g the number of controls to be put on the dialog comes from a
configuration file or the database.

I'd appreciate any help on how i put the user defined controls on the form
dynamically and how to manipulate the controls such as catching the click
event on them etc if they havent been put on the form at design time,

Thanks In Advance,
Macca

Nov 17 '05 #3

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

Similar topics

7
by: Bil Muh | last post by:
Esteemede Developers, I would like to Thank All of You in advance for your sincere guidances. I am developing a software using Visual C++ .NET Standard Edition with Windows Form (.NET)...
3
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
2
by: Sean | last post by:
Hello all, I may just not be searching for the right thing, but I've been looking for a way to dynamically create controls (and name them) in my code so that I can create only the controls I...
3
by: RSB | last post by:
Hi Every one Having tuff time creating web controls Dynamically. All i am trying to do is read a table and generate a list of ASP TEXT Box. So how do i create this Control dynamically and where...
0
by: tommy | last post by:
hello everybody, i haved read a few articles about, how to create controls in asp.net dynamically! i have tested it -- all works fine.... but....now it comes--> HOW create controls...
4
by: jesper_lofgren | last post by:
Hi there, I have some webcontrols that i want to add dynamically on a page. I have stored the path / namespace in database (ex MyNameSpace.WebControls.Control1) to the class/webcontrol. Lets...
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
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...
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,...
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...
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,...
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: 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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.