Create controls dynamically

M

macca

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
A

Andrej Tozon

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top