Q&A

Display Label Controls Vertically

Take advantage of the GDI+ graphics library to change the orientation of the label controls in an application; also, use Word's spell check from within your .NET application.

Technology Toolbox: C#

Q:
I'm developing a Windows application to manage financial information in C#. I have a WinForm with several controls, and I need to use every possible inch of space while guaranteeing that its resolution doesn't exceed 800-by-600. One thing that might help: Is there a way to display labels vertically, rather than horizontally?

A:
The .NET Framework contains a library of classes useful for managing graphical operations. They are known collectively as GDI+ classes, and they're powerful for creating advanced graphics such as a Windows form with irregular form, as well as charts, text effects, and much more. You can also use this library to place a label vertically (see Figure 1).

Three basic steps enable you to place a label vertically. First, inherit a new class from the Label class declared in the System.Windows.Forms namespace. This lets you keep those useful label characteristics such as the Text property and change only the way the text is rendered. Second, implement the Paint event handler to change the label's appearance. This function lets you retrieve the graphic information you need to change its appearance. Finally, you need to use the Graphics class's methods declared in the System.Drawing.Drawing2D namespace's Graphics class. Transform the text using the StringFormat class, then call the Draw method to draw the final text on the Windows form.

Accomplishing the first step is easy: Simply declare a new class that derives from the .NET Framework Label class:

public class VLabel : 
	System.Windows.Forms.Label

The base class supports a lot of useful events, methods, and properties your new class will inherit. One of these events, Paint, is raised when the label is about to be drawn on the screen. At this point, you need to change the label default behavior and draw it vertically instead (see Listing 1). The Paint event handler provides a PaintEventArgs parameter that contains a reference to the Graphics object associated with the label. The Graphics class is the main container of every graphic operation of every object on the Windows application, including Windows Forms. The final form control will be drawn as specified by its default properties if you don't change any Graphics properties. Changing some other property, whether from the Properties window or through code manually, changes only the property you specify.

Begin by storing the Graphics object provided by the Paint event handler into a local variable. Next, create a new StringFormat object to specify the text you want to run in the label. The FormatFlags property provided by this class lets you define the StringFormatFlags.DirectionVertical format flag, which lets you delegate the class to change the label orientation. Finally, call the DrawString method to draw the vertical label:

g.DrawString(
	this.Text,
	this.Font,
	textBrush,
	ClientRectangle,
	stringFormat);

The first parameter this method takes is the Label control's Text property. The second parameter specifies the font you want to write the text with, and the third parameter indicates the brush to use. The last two parameters indicate which region of the screen to draw the label on and the string format. The VLabel label uses the Text and Font properties set during design time and the ClientRectangle property inherited from the base class to retrieve the rectangle dimensions where you want the control drawn.

That's it for the essential steps, but you can download a sample that includes a property to flip the label's vertical appearance. It's a simple Boolean value that, when True, executes the RotateTransform and TranslateTransform methods provided by the Graphics class:

g.RotateTransform(180f);

g.TranslateTransform(
	-ClientRectangle.Width, 
	-ClientRectangle.Height);

The first method rotates the label 180 degrees, while the second performs a transform on the graphics matrix to specify the desired coordinates.

Featured

comments powered by Disqus

Subscribe on YouTube

Upcoming Training Events

0 AM
Live! 360 Orlando
November 17-22, 2024
TechMentor @ Microsoft HQ
August 11-15, 2025