Question Inherit usercontrol

Apperware

New member
Joined
Nov 25, 2008
Messages
1
Programming Experience
Beginner
using .net 2008,

1st i create a usercontrol
(add usercontrol after rightclicking on project), on this usercontrol i drag a label and set the text for this label. (uc1) > compile

2nd i create a class(add class after rightclicking on project), inherit usercontrol. compile and in designer drag a label and set text for the label (uc2) > compile

now when i create a form and add uc1 and uc2 to the form, only uc1 will show the text from the label i dragged onto it.. Why is this?

Hope someone can shed some light

Albert
 
The UserControl template has a hidden constructor that calls the InitializeComponent method. When you write a plain class that inherits UserControl the designer will also generate the InitializeComponent method, but it will not add a default constructor that calls it. So add a Sub New that calls InitializeComponent.

You will see similar if you add a constructor to the Form template, intellisense will automatically put a call to InitializeComponent method there with some comments that the Designer requires it.

Also have a look at the Designer generated code for UserControl template, when a component is added it requires a IContainer, which it a also adds for the class, but it also has default code that Dispose it, which you have to add yourself when not using the template.
 
Back
Top