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

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube