XTab's Blog

Ged Mead's Blog at vbCity

vbCity Blogs moved to:
http://cs.vbcity.com/blogs
  Home :: Syndication  :: Login

OctNovember 2009Dec
SMTWTFS
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Archives

Topics

Ramblings

VB.NET

  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:

 

 

Code Copy HideScrollFull
'  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:

 

Code Copy HideScrollFull
'  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:

 

Code Copy HideScrollFull
'  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.

 

Code Copy HideScrollFull
'  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:

 

 

Code Copy HideScrollFull
' 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.

 

Code Copy HideScrollFull
'  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:

 

Code Copy HideScrollFull
'  Housekeeping:
        b.Dispose()
        lg.Dispose()




. . .

 

Here’s the above code all in one lump, if you want to try it out:

 

 

Code Copy HideScrollFull
'  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.

posted on Wednesday, November 02, 2005 1:15 PM