locked
Superscript and subscript text in labels RRS feed

  • Question

  •  

    How do I create text with superscript and subscript characters in labels??
    Saturday, August 18, 2007 1:05 PM

Answers

  • I don't believe labels or textboxes support more than one font at a time.  Unless I'm mistaken, you can't change the font for a single character without affecting all the rest.  All you might be able to do is use separate labels and line them up next to each other, although that's pretty awkward solution and won't work if you need to change the text during the program run and the location of the superscripted character changes.

     

     

     

    Saturday, August 18, 2007 5:10 PM
  • Allthough Solitaire is right in that the standard label only supports a single font you can get round this by drawing the text yourself in the Paint event.  The following shows the general idea:

     

    Code Snippet

     

    Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint

    Dim F1 As New Font("Arial", 24, FontStyle.Regular)

    Dim F2 As New Font("Arial", 12, FontStyle.Regular)

    e.Graphics.DrawString("Normal", F1, Brushes.Black, 0, 0)

    Dim R1 As New RectangleF(New Point(0, 0), e.Graphics.MeasureString("Normal", F1, Integer.MaxValue, StringFormat.GenericTypographic))

    Dim X1 As Integer = CInt(R1.Right) + 5

    Dim Y As Integer = CInt(R1.Height / 2)

    e.Graphics.DrawString("Superscript", F2, Brushes.Black, X1, 0)

    Dim R2 As New RectangleF(New Point(X1, 0), e.Graphics.MeasureString("Superscript", F2, Integer.MaxValue, StringFormat.GenericTypographic))

    Dim X2 As Integer = CInt(R2.Right) + 5

    e.Graphics.DrawString("Subscript", F2, Brushes.Black, X2, Y)

    End Sub

     

     

    Saturday, August 18, 2007 5:53 PM

All replies

  • I don't believe labels or textboxes support more than one font at a time.  Unless I'm mistaken, you can't change the font for a single character without affecting all the rest.  All you might be able to do is use separate labels and line them up next to each other, although that's pretty awkward solution and won't work if you need to change the text during the program run and the location of the superscripted character changes.

     

     

     

    Saturday, August 18, 2007 5:10 PM
  • Allthough Solitaire is right in that the standard label only supports a single font you can get round this by drawing the text yourself in the Paint event.  The following shows the general idea:

     

    Code Snippet

     

    Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint

    Dim F1 As New Font("Arial", 24, FontStyle.Regular)

    Dim F2 As New Font("Arial", 12, FontStyle.Regular)

    e.Graphics.DrawString("Normal", F1, Brushes.Black, 0, 0)

    Dim R1 As New RectangleF(New Point(0, 0), e.Graphics.MeasureString("Normal", F1, Integer.MaxValue, StringFormat.GenericTypographic))

    Dim X1 As Integer = CInt(R1.Right) + 5

    Dim Y As Integer = CInt(R1.Height / 2)

    e.Graphics.DrawString("Superscript", F2, Brushes.Black, X1, 0)

    Dim R2 As New RectangleF(New Point(X1, 0), e.Graphics.MeasureString("Superscript", F2, Integer.MaxValue, StringFormat.GenericTypographic))

    Dim X2 As Integer = CInt(R2.Right) + 5

    e.Graphics.DrawString("Subscript", F2, Brushes.Black, X2, Y)

    End Sub

     

     

    Saturday, August 18, 2007 5:53 PM