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

  • Microsoft Revamps Fledgling AutoGen Framework for Agentic AI

    Only at v0.4, Microsoft's AutoGen framework for agentic AI -- the hottest new trend in AI development -- has already undergone a complete revamp, going to an asynchronous, event-driven architecture.

  • IDE Irony: Coding Errors Cause 'Critical' Vulnerability in Visual Studio

    In a larger-than-normal Patch Tuesday, Microsoft warned of a "critical" vulnerability in Visual Studio that should be fixed immediately if automatic patching isn't enabled, ironically caused by coding errors.

  • Building Blazor Applications

    A trio of Blazor experts will conduct a full-day workshop for devs to learn everything about the tech a a March developer conference in Las Vegas keynoted by Microsoft execs and featuring many Microsoft devs.

  • Gradient Boosting Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the gradient boosting regression technique, where the goal is to predict a single numeric value. Compared to existing library implementations of gradient boosting regression, a from-scratch implementation allows much easier customization and integration with other .NET systems.

  • Microsoft Execs to Tackle AI and Cloud in Dev Conference Keynotes

    AI unsurprisingly is all over keynotes that Microsoft execs will helm to kick off the Visual Studio Live! developer conference in Las Vegas, March 10-14, which the company described as "a must-attend event."

Subscribe on YouTube