Click here to Skip to main content
13,085,292 members (57,504 online)
Click here to Skip to main content
Add your own
alternative version


50 bookmarked
Posted 21 Aug 2003

Nice Line - A simple shaded line

, 21 Aug 2003
Rate this:
Please Sign up or sign in to vote.
A simple line control with multiple purposes

Demo showing uses off NiceLine


NiceLine is a pretty simple line that can be used to separate controls on a form or to make a title with a color background effect. The same effect can be obtained with 2 colored lines but is easier to just drag-drop this control on a form.

The code

The C# control class NiceLine is derived from System.Windows.Forms.UserControl. It overrides the OnPaint method to draw itself.

The main part of the code will draw the lines depending on the properties set (alignment, text caption, etc.)

// ------- 
// |      ...caption...
e.Graphics.DrawLines(new Pen(Color.DimGray, 1), 
new Point[] { 
    new Point(0, ym + 1), 
    new Point(0, ym), 
    new Point(beforeCaption, ym)

There are 5 properties that modify the appearance of NiceLine:

  • Caption - represents the caption text displayed on the line. If the caption is "" (the default) the line is not broken
  • CaptionMarginSpace - represents the distance in pixels form the control margin to caption text
  • CaptionPadding - represents the space in pixels around text caption
  • CaptionOrizontalAlign - will tell where the text caption is aligned in the control. Can be Left, Center or Right
  • LineVerticalAlign - represent the vertical alignment of the line within the space of the control. Can be Top, Middle or Bottom
/// The space in pixels around text caption
/// </SUMMARY>
[Description("The space in pixels around text caption")]
public int CaptionPadding
    get { return _CaptionPadding; }
        _CaptionPadding = value;

The LineVerticalAlign property require some explications. The necessity arrived because the control has a height that depend on the font chosen (to allow the text to be drown).

Control bonds and the line drown in middle

protected override void OnResize(System.EventArgs e)
    this.Height = this.Font.Height + 2;

So the line is drawn by default to the middle of the control but this can be changed with LineVerticalAlign property.

Title with background effect

Control bonds and the line drown in middle

This effect can be achieved using a Panel with the background color changed (in demo it's white), a colored label and a NiceLine control with LineVerticalAlign set to Top.

Transparent background

At the beginning I tried to make a transparent background (thus the LineVerticalAlign property is not necessary to make the title effect) but I ran in some problems in design mode (the control was not paining itself every time) so I decided to remove this. The code is still in the class (commented).


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Adrian Tosca
Web Developer
Romania Romania
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
manoj kumar choubey22-Feb-12 0:14
membermanoj kumar choubey22-Feb-12 0:14 
GeneralUsing a static control with a thinness of 2 Pin
Ziv G17-Jul-07 20:56
memberZiv G17-Jul-07 20:56 
GeneralAuto Resize Function Added Pin
PawJershauge5-Jan-07 3:12
memberPawJershauge5-Jan-07 3:12 
GeneralInteresting Pin
bsargos14-Nov-04 11:31
memberbsargos14-Nov-04 11:31 
GeneralSource doesn't work Pin
mikelb3-Oct-03 5:27
membermikelb3-Oct-03 5:27 
GeneralGood job... some suggestions Pin
Jacob Slusser25-Aug-03 13:15
memberJacob Slusser25-Aug-03 13:15 
GeneralRe: Good job... some suggestions Pin
Adrian Tosca25-Aug-03 23:10
memberAdrian Tosca25-Aug-03 23:10 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170813.1 | Last Updated 22 Aug 2003
Article Copyright 2003 by Adrian Tosca
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid