GUI reuse

  • Thread starter Thread starter sam williams
  • Start date Start date
S

sam williams

Hi,

I am wondering what the best practice in c# is for coding a resusable panel.
What I want to do is embed the widget into other forms/panels.
I'm new to c# coming from a java background. I think one way might be to add
form controls to a System.ComponentModel.Component.

The panel would contain something like:
__________________________
| Name [ ] Age[ ] |
| DOB [ ] etc.[ ] |
|__________________________|

and be embedded into other forms.

Thanks in advance.
 
I've used the UserControl class for just this. There are two UserControl
classes, one in Windows Forms, and the other under Web Forms. You want the
one in Windows Forms.

There is one complication to be aware of. You can create the user control
with the form designer, and drop it onto other forms with the form designer,
however this only works while the control has a parameterless constructor.
(the form designer still works for the UserControl, but you can't drop it
onto other controls) If you want to add a parameter to the constructor, then
place it on the the client form first, and save the code which the wizard
generates, then add the parameter. Once the parameter has been added, you
will lose all of the code which the designer generated.

Hmmm..reading what I've written, it looks to me as if the best solution is
to keep the parameterless constructor, for use by the form designer, and add
a second constructor with the parameter you want.. :).. I think I'll give it
a try.

Good luck,

Stephen
 
Back
Top