user interfaceWinForms Events for Inheriting Controls for UI Style

Follow up to this question:

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.

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

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.

Related Articles
  • I am new to WPF and I need a ribbon control for my app. I see there are lots of ribbon controls, it is confusing to choose which one and why to use it. Can you please suggest me which one to use? I use WPF and Visual Studio 2012, .NET 4.5. THanks Whe
  • HI, I'm extending a windows application written in C# to provide help to the user (in the context of the focused control) when they hit the F1 key. What I'd like to do is make use of the Control.HelpRequested event but I'm not sure how to extend all
  • What I want to do is Write a Method in WPF and say that the Target of this method is "X event" of "Y Control". Like Resources that you write a Resource Dictionary and say that Target of this resource is for example TextBoxes. For examp
  • Is there any way for the main form to be able to intercept events firing on a subcontrol on a user control? I've got a custom user-control embedded in the main Form of my application. The control contains various subcontrols that manipulate data, whi
  • I have a canvas inside a scrollview. I attached a keydown event handler to the scrollview. For most keys, the handler gets called. However, for the arrow keys, the handler does not get called. Instead, the scrollview gets scrolled in the appropriate
  • There are tons of questions and answers about Git versus TFVC Source Control, but no current answers given the integration of Git into Team Foundation Server/Service that I can find. I'm starting green pasture development using a wide variety of lang
  • Can someone recommend a version control engine + GUI that will be good for non-technical users? I'm perfectly fine with using Subversion with my team of developers, but I want to find something that will help the rest of our company. We are a design
  • I am building an applicaiton for the iPhone that will display upcoming and past events. I settled for a list view, but then I realized that a calendar (just like the one displayed in the "month" view in the built-in Calendar application) would b
  • This is a .net winform UI question. In many cases, we want to design an UI such that, you have several options, and based on the option you choose, a different detail UI is setup for you. For example, imagine a user registration: based on the locatio
You Might Also Like