Click here to Skip to main content
6,630,901 members and growing! (19,195 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Beginner License: The Code Project Open License (CPOL)

Extended Vertical Label Control in C# .NET

By /randz

A custom vertical label user control in C#.NET with support for transparent background.
C# 1.0, C# 2.0, Windows, .NET 1.0, .NET 1.1, .NET 2.0, WinForms, VS.NET2003, VS2005, Dev
Posted:26 Jul 2007
Updated:26 Sep 2007
Views:39,786
Bookmarked:56 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
5 votes for this article.
Popularity: 2.80 Rating: 4.00 out of 5

1

2
1 vote, 20.0%
3
3 votes, 60.0%
4
1 vote, 20.0%
5
Screenshot - VerticalLabel.jpg

Introduction

This article describes how to create a custom vertical label user control in C#.NET. The user control provides drawing of text from top or from bottom. This article is a derivation of Raman Tayal's Vertical Label Control in VB.NET. I just translated his work to C# and added the functionality of drawing the text starting from bottom.

Background

On one of my projects, I need a label control that can display text vertically. I encountered Raman Tayal's Vertical Label Control in VB.NET and translated it to C#. But I need additional functionality of drawing text starting from the top, so I just added the functionality. This control has been useful to me and I hope others would find it useful too.

Using the code

The code provided is a class that creates a dll that can be added as item in Toolbox of Windows Forms designer.

The class used the following namespaces:

using System;
using System.ComponentModel;
using System.Drawing;

Code

The part of the code that really does the job is the override for OnPaint event

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
    float vlblControlWidth;
    float vlblControlHeight;
    float vlblTransformX;
    float vlblTransformY;
    Color controlBackColor = BackColor;
    Pen labelBorderPen = new Pen(controlBackColor, 0);
    SolidBrush labelBackColorBrush = new SolidBrush(controlBackColor);
    SolidBrush labelForeColorBrush = new SolidBrush(base.ForeColor);
    base.OnPaint(e);
    vlblControlWidth = this.Size.Width;
    vlblControlHeight = this.Size.Height;
    e.Graphics.DrawRectangle(labelBorderPen, 0, 0, vlblControlWidth, vlblControlHeight);
    e.Graphics.FillRectangle(labelBackColorBrush, 0, 0, vlblControlWidth, vlblControlHeight);
            
            
    if (this.TextDrawMode == DrawMode.BottomUp)
    {
        vlblTransformX = 0;
        vlblTransformY = vlblControlHeight;
        e.Graphics.TranslateTransform(vlblTransformX, vlblTransformY);
        e.Graphics.RotateTransform(270);
        e.Graphics.DrawString(labelText, Font, labelForeColorBrush, 0, 0);
    }
    else
    {
        vlblTransformX = vlblControlWidth;
        vlblTransformY = vlblControlHeight;
        e.Graphics.TranslateTransform(vlblControlWidth, 0);
        e.Graphics.RotateTransform(90);
        e.Graphics.DrawString(labelText, Font, labelForeColorBrush, 0, 0, StringFormat.GenericTypographic);
    }
}

As you can see, I have an if condition in if (this.TextDrawMode == DrawMode.BottomUp). This tells is where the control decides whether to draw the text from bottom up or from top to bottom depending on the value of the property TextDrawMode.

When the value of TextDrawMode is BottomUp, you will notice that TranslateTransform accepts values zero for X component of translation and the height of the control as value for Y of the translation. This tells the GDI to start drawing from bottom left of the rectangle occupied by the control.

When the value of TextDrawMode is TopBottom, you will see that TranslateTransform accepts the control's width as X component of translation and zero as Y component of translation. This tells the GDI to start drawing from top right of the rectangle occupied by the control.

The TextDrawMode property is an additional property that can be set during design time and also during runtime.

On the screenshot that I provided, I deliberately changed the color of the background of the vertical label to show the anchor on where the control starts drawing the text.

Points of Interest

Since this is my first time to write a program using GDI+, I tried to do it using trial and error but it was frustrating at first, until I found a nice article on how to use the Graphics.RotateTransform.

History

July 27, 2007. Initial Version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

/randz


Member
I am working as as a fulltime Software Developer in Makati City, Philippines.
Occupation: Web Developer
Company: NNIT
Location: Philippines Philippines

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
GeneralThis is copied from another source PinmemberMonkeyCat5:41 28 Jan '09  
NewsI have posted previously an article on this subject!!! PinmemberHoria Tudosie4:58 6 Nov '08  
GeneralRe: I have posted previously an article on this subject!!! PinmvpJohn Simmons / outlaw programmer16:24 19 Nov '08  
GeneralThanks PinmemberSpikeAustin11:20 20 Aug '08  
GeneralRe: Thanks Pinmember/randz1:40 21 Aug '08  
QuestionLabel at Run-time PinmemberAlexB474:32 16 Nov '07  
AnswerRe: Label at Run-time Pinmember/randz16:03 18 Nov '07  
QuestionVertical control does not clear old text before writing new one Pinmemberdmcyu7:12 30 Oct '07  
AnswerRe: Vertical control does not clear old text before writing new one Pinmember/randz19:52 9 Nov '07  
AnswerRe: Vertical control does not clear old text before writing new one Pinmemberdmcyu5:24 11 Nov '07  
GeneralRe: Vertical control does not clear old text before writing new one Pinmember/randz14:43 13 Nov '07  
GeneralRe: Vertical control does not clear old text before writing new one Pinmemberdmcyu19:47 13 Nov '07  
GeneralRe: Vertical control does not clear old text before writing new one Pinmember/randz20:28 13 Nov '07  
GeneralRe: Vertical control does not clear old text before writing new one Pinmemberm0nk3yi3unz19:38 5 Oct '08  
GeneralTransparent Background PinmemberBenjaminHelbig22:28 25 Sep '07  
AnswerRe: Transparent Background [modified] Pinmember/randz17:31 26 Sep '07  
AnswerRe: Transparent Background PinmemberJinHwan4620:40 24 Jul '08  
GeneralWith anti-aliasing support Pinmemberflops426:49 8 Aug '07  
GeneralRe: With anti-aliasing support [modified] Pinmember/randz18:56 8 Aug '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Sep 2007
Editor:
Copyright 2007 by /randz
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project