Follow up to this question:
Winforms Style / UI Look and Feel Tips
So I have created my ‘base controls’ from which other controls inherit from. For testing, I am trying to change one of the base label’s font. But it is not propagating to the controls that inherit from it. On one of the forms, I can see the designer file is setting the properties for the control, so my base control’s properties are getting overridden.
On the base control’s I am using the Constructor to set the default properties. Should I be using a different event? If so, which one.
Here is the code for one of the base controls based on comment request…
Public Class InfoLabel Inherits Label Public Sub New() ' This call is required by the Windows Form Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. Me.Font = New System.Drawing.Font('Tahoma', 14.25!) Me.ForeColor = System.Drawing.Color.FromArgb(CType(CType(49, Byte), Integer), CType(CType(97, Byte), Integer), CType(CType(156, Byte), Integer)) Me.AutoSize = False End Sub End Class
The base controls show on the projects toolbox on the winform editor. Controls are then drag/drop from the toolbox.
Your problem is your custom control’s InitializeComponent() method. I have no idea why that is there. You would get that method automatically if you were implementing a UserControl, but inheriting from a standard control that method should not be there. With your base class having an InitializeComponent() method and your subclass also having one, someone is overwriting someone else.
I just subclassed a label in C#. I dragged this on my form and the font displayed as the new font, not the base (Label) class’s font.
I then created a second label, called MySubLabel which inherited from the MyLabel class. When I changed the ForeColor on the MyLabel class, the MySubLabel automatically updated.
So this should work.
Caveat: in Visual Studio you need to recompile the assembly before trying to see updates in the designer.