views:

469

answers:

1

Follow up to this question:

http://stackoverflow.com/questions/553518/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.

+1  A: 

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.

using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class MyLabel : Label
    {
        public MyLabel()
        {
            Font = new Font("Candara", 14);
        }
    }
}

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.

Chris Holmes
Agreed, no idea where InitializeComponent() came from. That's only for controls that have a custom designer, like Form and UserControl. Just comment it out of your code and it works as expected.
Hans Passant
I followed your steps and I can't get it to work. Is it VS drag/drop? Steps:1-Create new class, pasted your code.2-Compiled project3-drag/drop from toolbox onto form.4-it inherits settings as expected5-change base class prop. recompile.6-label that was drag/drop no change
B Z