Click here to Skip to main content
15,867,750 members
Articles / Desktop Programming / Windows Forms

How to set the DefaultValue for the Font property

Rate me:
Please Sign up or sign in to vote.
2.89/5 (18 votes)
12 Feb 2006MIT 63.7K   11   8
How to set the DefaultValue for the Font property.

Introduction

This article tells you about solving a very common problem in Visual Studio .NET 2003! You know that we could set the default value for some properties with the DefaultValue attribute, such as in the below command:

C#
[System.ComponentModel.DefaultValue(...)]

But if you want to set the default value for the Font property in this manner, you will see a very abnormal behavior and it doesn't work! Because, the Font property is not a simple property. I spent some time to solve this problem, and at last, I got the solution...

Scenario: Suppose that you want to create a button that is inherited from Microsoft Button, and you want to set the default value of the Font property to Tahoma with an 8 point size.

Solution: The below example will tell you how to do that:

C#
public class Button : System.Windows.Forms.Button
{
    private static System.Drawing.Font _defaultFont =
        new System.Drawing.Font("Tahoma",
        8.25f, System.Drawing.FontStyle.Regular,
        System.Drawing.GraphicsUnit.Point,
        178,
        false);

        public override System.Drawing.Font Font
        {
            get
            {
                return(base.Font);
            }
            set
            {
                if(value == null)
                    base.Font = _defaultFont;
                else
                {
                    if(value == System.Windows.Forms.Control.DefaultFont)
                        base.Font = _defaultFont;
                    else
                        base.Font = value;
                }
            }
        }

        public override void ResetFont()
        {
            Font = null;
        }

        private bool ShouldSerializeFont()
        {
            return(!Font.Equals(_defaultFont));
        }

        public Button()
        {
            Font = _defaultFont;
        }
    }

As you see in the above code, I did not write any attributes for the Font property, and instead, I used two methods with the names of ResetFont() and ShouldSerializeFont(). You should know that for each property in .NET, we have these two methods:

  • ResetSomeProperty()
  • ShouldSerializeSomeProperty()

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Web Developer Sematec Ins.
Iran (Islamic Republic of) Iran (Islamic Republic of)
My experiences are:

HTML 5.0, CSS 3.0
JQuery, Angular JS, Bootstrap

MVC 5.0, WEB API, c#

My Site URLs:
http://www.IranianExperts.ir
http://www.IranianExperts.com

My Yahoo Group URL: http://groups.yahoo.com/group/iranianexperts

Mobile: 0098-912-108-7461
Address: Tehran, Tehran, Iran

Comments and Discussions

 
QuestionCurrent Working Link Pin
Member 1426888411-Dec-22 6:20
Member 1426888411-Dec-22 6:20 
GeneralMy vote of 1 Pin
SylkoZ29-Jan-09 21:47
SylkoZ29-Jan-09 21:47 
AnswerLess complicated way PinPopular
Mike J.15-Apr-08 21:00
Mike J.15-Apr-08 21:00 
GeneralRe: Less complicated way Pin
WaferFun25-Nov-14 14:28
WaferFun25-Nov-14 14:28 
It worked. However it you want to inherit the Control that contains this property,you will gets the following error on the designer:
Object reference not set to an instance of an object.
at System.Windows.Forms.Control.AssignParent(Control value)
at System.Windows.Forms.Control.ControlCollection.Add(Control value)
at System.Windows.Forms.Design.DesignerFrame.Initialize(Control view)
at System.Windows.Forms.Design.DocumentDesigner.Initialize(IComponent component)
at System.ComponentModel.Design.DesignerHost.AddToContainerPostProcess(IComponent component, String name, IContainer containerToAddTo)
at System.ComponentModel.Design.DesignerHost.PerformAdd(IComponent component, String name)
at System.ComponentModel.Design.DesignerHost.System.ComponentModel.Design.IDesignerHost.CreateComponent(Type componentType, String name)
at System.ComponentModel.Design.Serialization.DesignerSerializationManager.CreateInstance(Type type, ICollection arguments, String name, Boolean addToContainer)
...
GeneralThanks... Pin
Vikas_kr6-Jun-07 1:36
Vikas_kr6-Jun-07 1:36 
Generalrelated MSDN article Pin
Mark Doughty28-Apr-06 8:38
Mark Doughty28-Apr-06 8:38 
JokeOh dear .... Pin
Corneliu Tusnea12-Feb-06 16:22
Corneliu Tusnea12-Feb-06 16:22 
GeneralRe: Oh dear .... Pin
eligazit21-Feb-06 23:19
eligazit21-Feb-06 23:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.