Click here to Skip to main content
15,559,491 members
Articles / Desktop Programming / Windows Forms
Article
Posted 12 Feb 2006

Stats

62.3K views
11 bookmarked

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
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 7:20
Member 1426888411-Dec-22 7:20 
GeneralMy vote of 1 Pin
SylkoZ29-Jan-09 22:47
SylkoZ29-Jan-09 22:47 
AnswerLess complicated way Pin
Mike J.15-Apr-08 22:00
Mike J.15-Apr-08 22:00 
GeneralRe: Less complicated way Pin
WaferFun25-Nov-14 15:28
WaferFun25-Nov-14 15:28 
GeneralThanks... Pin
Vikas_kr6-Jun-07 2:36
Vikas_kr6-Jun-07 2:36 
Generalrelated MSDN article Pin
Mark Doughty28-Apr-06 9:38
Mark Doughty28-Apr-06 9:38 
JokeOh dear .... Pin
Corneliu Tusnea12-Feb-06 17:22
Corneliu Tusnea12-Feb-06 17:22 
GeneralRe: Oh dear .... Pin
eligazit22-Feb-06 0:19
eligazit22-Feb-06 0: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.