473,763 Members | 7,727 Online
Bytes | Software Development & Data Engineering Community
+ 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 2541
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.co m

"macca" <as****@dsl.pip ex.com> wrote in message
news:dM******** ************@pi pex.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.Locati on = new System.Drawing. Point(0, 0);
textBox1.Name = "textBox1";
textBox1.TabInd ex = 1;
textBox1.Text = "Some default text";
textBox1.TextCh anged += new
System.EventHan dler(this.anyTe xtBox_TextChang ed);
Controls.Add(te xtBox1);

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_Text Changed(object sender, System.EventArg s e)
{
MessageBox.Show (((TextBox)send er).Text);
}

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

Andrej

"macca" <as****@dsl.pip ex.com> wrote in message
news:dM******** ************@pi pex.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
3556
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) template. Briefly -------------------------------------------------------------------------------------------- I need to create dynamically some controls on the forms, and display these
3
10806
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 then like that, when clicking the LinkButton, the user can be navigated to another page, carrying a variable. I would like to use server.transfer method instead of QueryString as I don't want the carried variable to be visible for the user.
2
332
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 need at run time. For example, I'm writing a filewatcher service that will eventually write back to a database, but I may need the service to watch multiple directories (thus, multiple FileSystemWatcher components). The rub is this...how do I...
3
2977
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 do i add the EventHandler to it. Thanks RSB
0
1093
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 dynamically using a html-table.... i want to display dynamically on each row one button, and dont use datagrid.
4
2159
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 say i have a column in database that looks like this. PageID (int) Webcontrol (varchar) 1 MyNameSpace.WebControls.Control1
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10144
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9937
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
3522
muto222
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.