Windows Develop Bookmark and Share   
 index > Windows Forms General > can a class derived from label has dashed line borderstyle?
 

can a class derived from label has dashed line borderstyle?

as the title
'cause the borderstyle of label are only three types(none,fixe3D,fixedsingle)
does anyone know how to make it has dashed line borderstyle?
spideywu  Tuesday, October 06, 2009 12:45 PM

You'd have to override the OnPaint method of the inherited control class and paint the border yourself.  Something like this might do it:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    Pen p = new Pen(Color.Blue);
    p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;

    Rectangle rect = new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width - 1,
                                   e.ClipRectangle.Height - 1);

    e.Graphics.DrawRectangle(p, rect);
}


Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki �LinkedIn �ForumsBrowser
David M Morton  Tuesday, October 06, 2009 12:55 PM
wow!
it works perfect!
thanks a lot

but i want the borderstyle be changed dynamically in run time,customer can change the style anytime 
hence sometimes it might be fixed3d or fixedsingle

if i put "if" in OnPaint, will it be not appropriate? (may slow down pc?)

thanks again!
spideywu  Tuesday, October 06, 2009 1:07 PM

Create two properties: one for BorderColor, and another one for BorderStyle.  Have the method use the values from these properties instead of hard-coding the settings.  Change the property to change the way the label appears. 

public class DashedLabel : Label
{
    public DashedLabel()
    {
        DashStyle = DashStyle.Dash;
        BorderColor = Color.Black;
    }

    public DashStyle DashStyle { get; set; }
    public Color BorderColor { get; set;  }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        Pen p = new Pen(BorderColor);
        p.DashStyle = DashStyle;

        Rectangle rect = new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width - 1,
                                       e.ClipRectangle.Height - 1);

        e.Graphics.DrawRectangle(p, rect);
    }

}


Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki �LinkedIn �ForumsBrowser
David M Morton  Tuesday, October 06, 2009 1:12 PM
i got you
but what if customer want to change the borderstyle to NONE or fixedsingle?

spideywu  Tuesday, October 06, 2009 1:30 PM
Have another property that is a boolean value to enable or disable custom borders.  If it's false, don't draw the borders - the default border specified by the BorderStyle property should show up.  If it's true, draw the custom borders.  
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki �LinkedIn �ForumsBrowser
David M Morton  Tuesday, October 06, 2009 1:45 PM
thank you very much!!!
you help me a lot:)))
spideywu  Tuesday, October 06, 2009 3:43 PM

You can use google to search for other answers

Custom Search

More Threads

• printing out of a richtextbox control
• Updating data on multiple applications when value change
• Anyone want a challenge? several errors with DatagridView (C++ code supplied)
• Override??
• why is are there no rows in the tables of myDataSet
• MaskedTextBox alignment problem
• axWindowsMediaPlayer with custom contextMenuStrip
• A huge huge bug? Exception swallowed silently!
• Multiline Textbox
• How to: Provide a Progress Dialog Box for File Operations