By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,065 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

Create controls dynamically

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.