Click here to Skip to main content
12,503,978 members (54,024 online)
Click here to Skip to main content
Add your own
alternative version


33 bookmarked

Gradient Label Control For WinForms

, 10 May 2003
Rate this:
Please Sign up or sign in to vote.
Create background gradient color bar

Sample Image - GradientLabel.jpg


As all of us know GDI+ which is supported by .NET has many new features for graphics. One of them is the gradient brush. Gradient brushes can also be used to draw lines, curves, and paths. You can use a linear gradient brush to fill a shape with color that changes gradually as you move across the shape. For example, suppose you create a horizontal gradient brush by specifying blue at the left edge of a shape and green at the right edge. When you fill that shape with the horizontal gradient brush, the color changes gradually from blue to green as you move from the shape's left edge to its right edge.

I think it is very useful and so I have written a control called GLabel that inherits from Label control of System.Windows.Forms. This GLabel has backgound with gradient color. Because it is a linear gradient it needs two colors; Left color and Right color for linear gradient. When you use it on a WinForm, you should set two colors via properties BeginColor and EndColor of GLabel class.

Code listing

Following is this GLabel class:

public class GLabel : System.Windows.Forms.Label
 // declare two color for linear gradient
  private Color cLeft;
  private Color cRight;

   // property of begin color in linear gradient
  public Color BeginColor
      return cLeft;
      cLeft = value;
   // property of end color in linear gradient
  public Color EndColor
      return cRight;
      cRight = value;
  public GLabel()
     // Default get system color 
    cLeft = SystemColors.ActiveCaption;
    cRight = SystemColors.Control;
  protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
     // declare linear gradient brush for fill background of label
    LinearGradientBrush GBrush = new LinearGradientBrush(
        new Point(0, 0),
        new Point(this.Width, 0), cLeft, cRight);
    Rectangle rect = new Rectangle(0,0,this.Width,this.Height);
     // Fill with gradient 
    e.Graphics.FillRectangle(GBrush, rect);

     // draw text on label
    SolidBrush drawBrush = new SolidBrush(this.ForeColor);
    StringFormat sf = new StringFormat();
     // align with center
    sf.Alignment = StringAlignment.Center;
     // set rectangle bound text
    RectangleF rectF = new 
     // output string
    e.Graphics.DrawString(this.Text, this.Font, drawBrush, rectF, sf);

This class above is simple. You can develop it to serve your ideas. You can rewrite OnPaint as well, such as write more alignment styles for text. This code is written fixed for the top-center in rectangle (this rectangle contains rectangle of GLabel at center). When you put GLabel control in a WinForm you can write code as shown below:

public class Form1 : System.Windows.Forms.Form
  private GLabel gLabel1;
  private System.ComponentModel.Container components = null;  
  public Form1()
    //Required for Windows Form Designer support
    this.gLabel1 = new GLabel();
    this.gLabel1.BeginColor = System.Drawing.SystemColors.ActiveCaption;
    this.gLabel1.EndColor = System.Drawing.SystemColors.Control;
    this.gLabel1.Name = "gLabel1";
    this.gLabel1.Font = new Font("Verdana", 12F, FontStyle.Bold );
    this.gLabel1.Size = new System.Drawing.Size(350, 50);
    this.gLabel1.TabIndex = 0;
    this.gLabel1.Text = "Nguyen Ha Giang - Vietnam - HCMC";


That's all, thanks a lot for reading from Nguyen Ha Giang, Vietnam, HCM City.


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

Web Developer
Vietnam Vietnam
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
al6k2-Jun-11 2:54
memberal6k2-Jun-11 2:54 
GeneralHello from HCMC Pin
seanco15-Oct-06 20:55
memberseanco15-Oct-06 20:55 
GeneralDon't repeat Pin
Meisi11-May-03 17:08
memberMeisi11-May-03 17:08 
General[Msg Deleted] Pin
HaGiang11-May-03 22:09
memberHaGiang11-May-03 22:09 
GeneralRe: Don't repeat Pin
Meisi12-May-03 17:53
memberMeisi12-May-03 17:53 
GeneralRe: Don't repeat Pin
Anonymous5-Nov-03 10:13
sussAnonymous5-Nov-03 10:13 
GeneralProblems Pin
Steve McLenithan11-May-03 13:01
memberSteve McLenithan11-May-03 13:01 
GeneralRe: Problems Pin
Carlos H. Perez11-May-03 13:11
memberCarlos H. Perez11-May-03 13:11 
GeneralRe: Problems Pin
Marc Clifton11-May-03 13:22
memberMarc Clifton11-May-03 13:22 
Carlos H. Perez wrote:
Why the big rush to publish?


1) immediate gratification
2) it can reduce consumption of editor CPU cycles
3) it's a bit more WYSIWYG. While I write my articles off-line in front-page (ugh), the final touchups have to happen interactively, so the preview mode is great.
4) To paraphrase Jesus--there will always be bad articles.

Does an article like this ever get edited? The grammar/sentence structure is so bad that the editor would essentially be rewriting the content, guessing at what the author had in mind. I'm not sure that's an editor's job. Is it? That said, I don't think one should be faulted on bad English when it's obviously a struggle for that person. I guess in my more generous moods I'd give the person credit for at least trying. What really gets me is when an English speaking person posts a crappy article, or the article hasn't even been checked for valid images and download links.


Help! I'm an AI running around in someone's f*cked up universe simulator.
Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
Every line of code is a liability - Taka Muraoka
Microsoft deliberately adds arbitrary layers of complexity to make it difficult to deliver Windows features on non-Windows platforms--Microsoft's "Halloween files"

GeneralRe: Problems Pin
WREY12-May-03 6:55
memberWREY12-May-03 6:55 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160919.1 | Last Updated 11 May 2003
Article Copyright 2003 by nhg2k
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid