Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#
Article

Text Alignment Control for .NET

Rate me:
Please Sign up or sign in to vote.
4.22/5 (15 votes)
15 Sep 20042 min read 64.5K   2K   37   10
An article on creating Text Alignment Control for .net

Introduction

After a long article absence, I've climbed back into the 'Code' saddle again to bring you some new articles specifically converting .net. I've started with a relatively easy article covering a text alignment control, eye candy being a specialty of mine.

Requirements

The article expects the reader to be familiar with C#, GDI+ and the .net framework. The project was developed using Visual Studio 2003.

Motivation

This control was written to mimic the Text Alignment control found in Microsoft Excel. It was created to add user interface cue for aligning text, this type of control does not exist for in the .net framework.

Benefits over the Microsoft Excel Text Alignment control are:

  • Visual cues for selected and disabled.
  • Input to allow Up/Down (increment by +1/-1 degree) and control Up/Down (increment by +15/-15 degrees).
  • Home to reset to 0 degrees.

Design

The design on the control is relatively straight forward, the control is inherited from a User control and the only real points worth mentioning is the logical for mapping from a point to angle and visa versa and the logic involved in rotating the text. These are internal (private) functions and are detailed are below:

C#
private Point _AngleToPoint(Point ptOffset, double angle, double radius) 
{
    double radians = angle / (180.0 / Math.PI);
    int x = ptOffset.X + (int)((double)radius* Math.Cos(radians));
    int y = ptOffset.Y - (int)((double)radius * Math.Sin(radians));
    return new Point(x,y);
}
C#
private double _ArcTangent(double ratio) 
{
    double angle = Math.Atan(ratio);

    // convert radians to degrees 
    eturn angle * (180.0 / Math.PI);;
}

and the GDI+ code used to rotate the text

C#
...
g.TranslateTransform (ptCenter.X, ptCenter.Y);
g.RotateTransform(-_Angle);
g.DrawString("Text", Control.DefaultFont, SystemBrushes.WindowText, 
  new Point(0,(int)-(sz.Height/2)), format);
g.ResetTransform();
...

Using the code

The control is presented as a Windows Control Library, so once compiled it's easy to start using the code, create new C# Windows Project and open in main form, select the "My User Controls" tab and "Add/Remove Items..." and select the TextAlignCtrl.dll.

You toolbox should contain the text alignment control see below:

There is one property to Get/Set the angle on the control, this is appropriately name "Angle", there is also one event which can be sink when the angle has changed, the is called "OnAngleChangedEvent".

In the demo code you can see the control being used in conjunction with numeric control and canvas control (Label based) to UI feedback.

That wraps it up, simplistic, yet effective, I haven't seen a text alignment control for windows, so this should fill that niche - enjoy!

History

  • Version 1.0 - Origin

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


Written By
Software Developer (Senior) Software Kinetics
United Kingdom United Kingdom




Software Kinetics
are experts in developing customised and bespoke applications and have expertise in the development of desktop, mobile and internet applications on Windows.


We specialise in:

  • User Interface Design
  • Desktop Development
  • Windows Phone Development
  • Windows Presentation Framework
  • Windows Forms
  • Windows Communication Framework
  • Windows Services
  • Network Applications
  • Database Applications
  • Web Development
  • Web Services
  • Silverlight
  • ASP.net


Visit Software Kinetics

Comments and Discussions

 
QuestionConverting this to an Label? Pin
Tim8w10-Dec-20 7:49
Tim8w10-Dec-20 7:49 
GeneralReally nice! Pin
seq-12-Feb-07 9:34
seq-12-Feb-07 9:34 
GeneralRe: Really nice! Pin
NormDroid23-Feb-07 1:54
professionalNormDroid23-Feb-07 1:54 
GeneralGreat article! Pin
kryzchek5-Dec-05 5:02
kryzchek5-Dec-05 5:02 
GeneralRe: Great article! Pin
NormDroid5-Dec-05 20:54
professionalNormDroid5-Dec-05 20:54 
You're Welcome!

Blogless
GeneralGood Pin
vivekthangaswamy16-Sep-04 0:57
professionalvivekthangaswamy16-Sep-04 0:57 
GeneralRe: Good Pin
NormDroid16-Sep-04 1:08
professionalNormDroid16-Sep-04 1:08 
GeneralDivision performance Pin
Simon Hughes15-Sep-04 21:34
Simon Hughes15-Sep-04 21:34 
GeneralRe: Division performance Pin
Simon Hughes15-Sep-04 21:44
Simon Hughes15-Sep-04 21:44 
GeneralRe: Division performance Pin
NormDroid15-Sep-04 21:58
professionalNormDroid15-Sep-04 21:58 

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.