Click here to Skip to main content
Licence MIT
First Posted 12 Feb 2006
Views 34,932
Bookmarked 10 times

How to set the DefaultValue for the Font property

By | 12 Feb 2006 | Article
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:

[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:

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

About the Author

Dariush Tasdighi

Web Developer

Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member

I'm experienced in below items:
 
- XML 1.0
- CSS 2.0
- ASP 3.0
- HTML 4.01
- XHTML 1.0
- Javascript 1.5
- .NET Framework 1.1/2.0
- Microsoft Office 2000/XP
- Microsoft Visual Basic 6
- Microsoft SQL Server 2000/2005
- Microsoft C#.NET (Windows Based)
- Microsoft C#.NET (XML Web Service)
- Microsoft C#.NET (Web Based = ASP.NET)
 
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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberSylkoZ21:47 29 Jan '09  
AnswerLess complicated way PinPopularmemberMike J.21:00 15 Apr '08  
GeneralThanks... PinmemberVikas_kr1:36 6 Jun '07  
Generalrelated MSDN article PinmemberMark Doughty8:38 28 Apr '06  
JokeOh dear .... PinmemberTutu16:22 12 Feb '06  
GeneralRe: Oh dear .... Pinmembereligazit23:19 21 Feb '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 12 Feb 2006
Article Copyright 2006 by Dariush Tasdighi
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid