1. Putting the code inside the Label’s Paint event ensures that the text is refreshed every time the label has to be redrawn. This can happen if, for example, the form is minimized or another form or even another application is opened on top of it, causing it to be obscured.
Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
2. Next is the text that we want to display vertically in the label.
Dim strText As String = "Top to Bottom"
3. The Label control has a Graphics object with which it is associated. Without getting too deep into the technicalities, this can be thought of as the canvas on which the control’s text and images are drawn and it is the same size and shape as the control itself.
There’s more to it than that, but if you think of the Graphics object in that way, you oftentimes won’t need to delve any further into its complexities.
Dim lbl1gfx As Graphics = e.Graphics
So in this case, we can get hold of the Graphics object for this particular control by extracting it from the
ByVal e As System.Windows.Forms.PaintEventArgs
element of the event’s parameters.
4. Although we are all very familiar with the use of fonts in general, we don’t often have to get involved with them as objects in their own right. We’ve been pretty well led by the hand most times, having to do no more than choose a font, a font color, a font size and a style from a dropdown list.
When hand building text in the way that we are with DrawString, you also have to create an instance of the particular font that you want to use. This is one of those rare occasions where Intellisense doesn’t lead you by the hand every step of the way. It’s still a big help, of course, but you have to type in the name of the Font family you want to use (i.e. there isn’t a list for you to choose from). It’s not asking a lot of the developer, but we do tend to get used to having it all done for us, so you need to be ready for that.
The only other potential gotcha with creating a font is that sometimes – but only sometimes - the permutations of arguments don’t reveal themselves in exactly the way you might expect. That is, you may be expecting to enter a font size as the next parameter, but when you look up and check the screen you may see that it has jumped straight to the FontStyle parameter. It’s not the biggest problem in the world, but something to watch out for.
Font sizes are listed as “emSize”, by the way, in Intellisense when instantiating Font object instances.
So, the code line below sets the Font Family to Verdana, Font Size to 12, and the FontStyle to Regular (that is, not Bold, Underlined, Italic, etc).
Dim fnt As Font = New Font("Verdana", 12, FontStyle.Regular)
5. StringFormat is a Class within the Drawing Class that offers you choices of text layout, including alignment and spacing. Alignment is of course the area we are interested in at the moment. The first line above creates a new instance of a StringFormat object.
Within the StringFormat class, there is a Property called FormatFlags. We can access this property and select one or more of the choices from the Enumeration of StringFormatFlags on offer. The second line of code therefore selects the DirectionVertical choice from the enumeration and assigns it to the StringFormat instance we created and named SF.
Dim SF As New StringFormat
SF.FormatFlags = StringFormatFlags.DirectionVertical
6. The next line isn’t always necessary. However if you are going to replace one text display with something different then you will need to Clear the Graphics object first, otherwise the second text string will be appended to the first one.
You can clear the label to any backcolor you choose.
7. The next line is also optional, but recommended. TextRenderingHints offer a small range of choices of text quality. In general - as with most things processing based – the better the quality demanded, the bigger the potential performance hit. For a tiny code example such as this one, the processing performance reduction is negligible.
The AntiAlias option used in this example is often the best quality/performance choice for general application work.
Note that if you decide that you don’t want to use a TextRenderingHint then you can also dispense with the Imports statement I recommended earlier that you place at the top of the form.
lbl1gfx.TextRenderingHint = _ TextRenderingHint.AntiAlias
8. And, finally, the text is “written” or drawn using DrawString. The parameters are Text string, Font, Brush used to draw(write) with, String Origin X-Position, String Origin Y-Position, StringFormat.
We have already dealt with the text string and the font.
The brush used is a basic system defined Brush object: you can select any of the known colors for the brush by picking the one of your choice from the drop down list in Intellisense.
The String Origin values represent the number of pixels from the left hand side of the label (X-Position) and from the top of the label (Y-Position) from where the text drawing starts.
lbl1gfx.DrawString(strText, fnt, Brushes.Black, 12, 12, SF)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
As you can see from the step by step code, it’s all fairly straightforward and you have several choices and options open to you.
Although this example uses a label control, you are not limited to this. You can use the same technique with minimal changes to create the same display on the surface of a form, a Button, a PictureBox or any other control which has a Text property.