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.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube