Here’s a nifty little tip that you may possibly find useful one day . I had to work out how to display superscript and subscript characters using the DrawString method. Now, Drawstring itself is a fairly easy beast to tame once you’ve done it a couple of times, but the extra requirement of the super and subscript digits needed an extra bit of thought.
Anyhow, here’s how I went about it:
I created two separate fonts, one for the standard size/position text and one for the superscript:
' A font for the standard size text:
Dim fnt As Font = New Font("Verdana", 13, FontStyle.Regular)
' A smaller font for the superscript part
Dim smallfnt As New Font("Verdana", 8, FontStyle.Regular)
. . .
Then we need a brush to write with and somewhere to write. In this case I was drawing the string on a label named Label1:
' A Brush to write the text:
Dim b As Brush = New SolidBrush(Color.Black)
' Graphics object
Dim lg As Graphics = Label1.CreateGraphics
. . .
For code clarity and reusability, I created variables to hold the standard text and the superscript separately:
' Text variables and values
Dim Name As String = "XY"
Dim Number As String = "3"
. . .
Now we can calculate the width of the Name part of the display.
Why do we need this? So that - after we have drawn the Name part - we know exactly where the Number (the Superscript bit) should be drawn.
' Calculate the size of the Name part
Dim TextSize As New SizeF(lg.MeasureString(Name, fnt))
' and note the width
Dim wordwidth As Integer = CInt(TextSize.Width)
. . .
The next step is to draw the Name part:
' Ensure that label is empty before we start:
lg.Clear(Color.White)
' Draw the name
lg.DrawString(Name, fnt, b, 10, 10)
. . .
You can change the start point of the text (currently location 10, 10) on the label to suit your particular requirements.
The superscript text can now be drawn at a location which is 5 pixels to the right of the Name text and 4 pixels higher. We use the smaller font for this.
' Now you can calculate where the superscript part should be ...
' and draw it
lg.DrawString(Number, smallfnt, b, wordwidth + 5, 6)
Finally, the standard housekeeping to dispose of finished objects:
' Housekeeping:
b.Dispose()
lg.Dispose()
. . .
Here’s the above code all in one lump, if you want to try it out:
' A font for the standard size text:
Dim fnt As Font = New Font("Verdana", 13, FontStyle.Regular)
' A smaller font for the superscript part
Dim smallfnt As New Font("Verdana", 8, FontStyle.Regular)
' A Brush to write the text:
Dim b As Brush = New SolidBrush(Color.Black)
' Graphics object
Dim lg As Graphics = Label1.CreateGraphics
' Text variables and values
Dim Name As String = "XY"
Dim Number As String = "3"
' Calculate the size of the name part
Dim TextSize As New SizeF(lg.MeasureString(Name, fnt))
' and identify the width it will occupy
Dim wordwidth As Integer = CInt(TextSize.Width)
' Ensure that label is empty before we start:
lg.Clear(Color.White)
' Draw the name
lg.DrawString(Name, fnt, b, 10, 10)
' Now you can calculate where the superscript part should be ...
' and draw it
lg.DrawString(Number, smallfnt, b, wordwidth + 5, 6)
' Housekeeping:
b.Dispose()
lg.Dispose()
. . .
I think that if I was going to use this a lot, I would tweak it further by:
a. Creating variables for the x and y positions, in place of the hard coded numbers I’ve used here.
b. Turning it into a procedure (it’s currently in a button click event), passing in the various values needed, so increasing the reusability of the thing.