Click here to Skip to main content
Licence 
First Posted 26 Sep 2004
Views 108,125
Bookmarked 71 times

Customized Text - Orientated Controls in C# - Part I (Label Control)

By | 26 Sep 2004 | Article
Label which can display text in any orientation and shape.

Sample Image - customtext.jpg

Introduction

There are many ways in which text can be displayed. But most common ways are showing it in the path of straight line, arc, and circle. The Label which comes with NET can only display the text in the path of a straight line. This control can do that in all the main three methods, with rotation angle. Also, text can be displayed in a direction which we are interested in.

How it Works

There are a few points we must take care about, before we write:

  1. Where we must write the character.
  2. Angle of the letter with the axis.
  3. Path of the string.
  4. Direction of the string.

According to our needs, we can override the control's OnPaint method and draw the text. We must draw the text on our own using Graphics object. It has very useful methods which can be used here. TranslateTransform and RotateTransform are the common methods used to achieve this task. TranslateTransform is used to move the text, where we want it to be displayed. The other method is to rotate the character to orientate it with center.

The control has three properties, which can be used:

  • TextOrientation

    How the text must be displayed. Three methods are supported in Label. They are Rotate, Arc and Circle. We can display it in a straight line with rotation. I mean, the text will be displayed in a straight path, but the text can be rotated in any angle we want. The other is Arc, in which we can display a text in arc shape. The last one is, displaying it in circular path.

  • TextDirection

    There are two ways, which direction we want to display text when we think about displaying it in arc's path or circular one. They are clockwise and anticlockwise.

  • RotationAngle

    Text can be rotated in any angle we like. This can be mostly done in straight line.

Summary

My plan is to write custom controls in which we can rotate the text. In future, according to the feedback I get from this, I will write more custom controls where we can do text oriented works.

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

Chester Ragel

Web Developer

Singapore Singapore

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionalignment problem Pinmemberamolpbhavsar1:27 18 Oct '11  
GeneralText gets messed up when using it in other user control with scrollbox... PinmemberNicklez3:06 28 Feb '11  
GeneralRe: Text gets messed up when using it in other user control with scrollbox... PinmemberSteveproject1:32 25 Jun '11  
GeneralWinCE Compact framework Pinmemberarunmuthu2:25 13 Oct '10  
GeneralThanks Pinmemberarapillai0:14 9 Nov '09  
GeneralSimple/Perfect. Thank you! PinmemberDouglas Berry5:28 9 Oct '08  
GeneralYes I love it. PinmemberV# Guy1:39 23 Jun '08  
GeneralRe: Yes I love it. PinmemberAhmad9919:22 3 Sep '08  
QuestionHow to modify this control for web forms. PinmemberAhmad993:01 4 Jun '08  
AnswerRe: How to modify this control for web forms. PinmemberElliott Ortiz6:17 12 Oct '09  
GeneralLabelled Textbox PinmemberChakreee4:00 8 Sep '06  
Generalmake it support colors PinmemberTheCardinal12:30 11 Aug '06  
GeneralI need rtf codes to include picture PinsussAnonymous8:55 10 Oct '05  
QuestionGreat Control, but... Pinmemberlgbjr21:01 20 Sep '05  
GeneralEntryPoint Question PinsussAnonymous6:39 11 Jun '05  
GeneralI need help PinmemberTomek Frykas5:18 11 Jun '05  
GeneralProblems with alignement Pinmemberdscemama2:32 13 Apr '05  
GeneralRe: Problems with alignement Pinmemberdscemama3:13 13 Apr '05  
Here is the solution I came to in the Paint event:
 
...
case Orientation.Rotate :
{
//For rotation, who about rotation?
double angle = (rotationAngle/180)*Math.PI;
StringFormat stringFormat = new StringFormat();
stringFormat.Trimming = StringTrimming.None;
switch (this.TextAlign)
{
case ContentAlignment.BottomLeft:
case ContentAlignment.MiddleLeft:
case ContentAlignment.TopLeft:
{
stringFormat.Alignment = StringAlignment.Near;
break;
}
 
case ContentAlignment.BottomCenter:
case ContentAlignment.MiddleCenter:
case ContentAlignment.TopCenter:
{
stringFormat.Alignment = StringAlignment.Center;
break;
}
 
case ContentAlignment.BottomRight:
case ContentAlignment.MiddleRight:
case ContentAlignment.TopRight:
{
stringFormat.Alignment = StringAlignment.Far;
break;
}
}
 
graphics.TranslateTransform(
(ClientRectangle.Width+(float)(height*Math.Sin(angle))-(float)(width*Math.Cos(angle)))/2,
(ClientRectangle.Height-(float)(height*Math.Cos(angle))-(float)(width*Math.Sin(angle)))/2);
graphics.RotateTransform((float)rotationAngle);
graphics.DrawString(text,this.Font,textBrush,graphics.ClipBounds, stringFormat);
graphics.ResetTransform();
 
break;
}
...
 
It seems to work so far.
 
David

GeneralRe: Problems with alignement Pinmemberlowrider19682:36 15 Dec '05  
GeneralRe: Problems with alignement Pinmemberni123459:20 2 Nov '10  
GeneralJust what I was looking for! PinmemberCCosgrove5:13 18 Feb '05  
GeneralOrientated Textbox PinmemberGreg Schroder10:12 30 Jan '05  
GeneralI need Help Pinmemberxkudos0:35 1 Nov '04  
GeneralRe: I need Help PinmemberChester Ragel0:41 1 Nov '04  
GeneralRe: I need Help Pinmemberxkudos0:53 1 Nov '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120528.1 | Last Updated 27 Sep 2004
Article Copyright 2004 by Chester Ragel
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid