5,137,506 members and growing! (12,322 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Beginner

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, C#, Windows, .NET, .NET 1.0, .NET 1.1, .NET 2.0, WinForms, VS.NET2002, VS.NET2003, VS2005, VS, Dev

Posted: 26 Jul 2007
Updated: 26 Sep 2007
Views: 14,879
Announcements



Search    
Advanced Search
Sitemap
4 votes for this Article.
Popularity: 2.37 Rating: 3.94 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 25.0%
3
3 votes, 75.0%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article
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 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

/randz


I am working as as a fulltime Software Developer in Makati City, Philippines.

Occupation: Web Developer
Location: Philippines Philippines

Other popular Miscellaneous articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionLabel at Run-timememberAlexB474:32 16 Nov '07  
AnswerRe: Label at Run-timemember/randz16:03 18 Nov '07  
QuestionVertical control does not clear old text before writing new onememberdmcyu7:12 30 Oct '07  
AnswerRe: Vertical control does not clear old text before writing new onemember/randz19:52 9 Nov '07  
AnswerRe: Vertical control does not clear old text before writing new onememberdmcyu5:24 11 Nov '07  
GeneralRe: Vertical control does not clear old text before writing new onemember/randz14:43 13 Nov '07  
GeneralRe: Vertical control does not clear old text before writing new onememberdmcyu19:47 13 Nov '07  
GeneralRe: Vertical control does not clear old text before writing new onemember/randz20:28 13 Nov '07  
GeneralTransparent BackgroundmemberBenjaminHelbig22:28 25 Sep '07  
AnswerRe: Transparent Background [modified]member/randz17:31 26 Sep '07  
GeneralWith anti-aliasing supportmemberflops426:49 8 Aug '07  
GeneralRe: With anti-aliasing support [modified]member/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-2008
Web20 | Advertise on the Code Project