开发者

Styling C# forms

I am developing a C# Windows application. It includes 22 forms for styling (i.e. font, backcolor,italic,cell color,text fore color) controls. I have created a function to do this I am getting an errer.

My code:

foreach (Control cnn in gbsty.Controls)
{
    if (cnn is Label)
    {
        if (cnn.Name=="lblheadername")
        {
            cnn.Font = FontStyle.Bold;
        }
    }
}

Error:

Cannot implicitly convert type 'System.Drawing.FontStyle' to 'System.D开发者_开发知识库rawing.Font'


cnn.Font = new Font(cnn.Font, FontStyle.Bold);

See more constructors on MSDN


Additionally, if you want to have common font, label, textbox, etc... it might be better for you to create your own class library (as I have done, and probably others too) that are derived from the base class (such as label, textbox, etc) and force their default font setting to a given size, color, etc. Then add THOSE to your forms and they will all have same styles applied.

To prevent the IDE from actually saving the Font information (or other as you customize) within the form and get directly from the class definition, you could do one of two ways (that I am aware of)... Ex: under C#

public class MyCustomLabel : Label
 {
    // Either use the [DesignerSerialization...]
    // OR override and make the FONT as READ-ONLY (via only a GETTER)
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override Font Font
    { get { return new Font("Arial", 9F, FontStyle.Regular, GraphicsUnit.Point);; } }

    ...
    ...

}

If read-only, the form designers will nag about an error on the value being read-only as it was originally put into the form, but once you remove that "Font" element, you'll never have to see that message again. Then, if you want to change such font, size, color, etc, Just change it in YOUR baseclass.

Additionally, from this, if you wanted to create a "HEADER" type of label as bold or other, just derive from YOUR baseclass and supercede the FONT definition again, but with ITS bold/size/color settings...

public class MyHeaderLabel : MyCustomLabel
{
   public override Font Font
   { get { return new Font("Arial", 12F, FontStyle.Bold, GraphicsUnit.Point); }

}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜