none
Superscript and Subscript RRS feed

  • Question

  • I am having trouble with superscript and subscript with a richtextbox.

    I have this so far for superscript:

    Dim currentFont As System.Drawing.Font = RichTextBox1.SelectionFont
           
            If RichTextBox1.SelectionCharOffset = 10 Then
                RichTextBox1.SelectionCharOffset = 0
            ElseIf RichTextBox1.SelectionCharOffset = 0 Then
                RichTextBox1.SelectionCharOffset = 10
                RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont.FontFamily, currentFont.Size - 5, RichTextBox1.SelectionFont.Style)
            ElseIf RichTextBox1.SelectionCharOffset = -10 Then
                RichTextBox1.SelectionCharOffset = 10
            End If

    And this for subscript:

    If RichTextBox1.SelectionCharOffset = -10 Then
                RichTextBox1.SelectionCharOffset = 0
            ElseIf RichTextBox1.SelectionCharOffset = 0 Then
                RichTextBox1.SelectionCharOffset = -10
    	    RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont.FontFamily, currentFont.Size - 5, RichTextBox1.SelectionFont.Style)
            ElseIf RichTextBox1.SelectionCharOffset = 10 Then
                RichTextBox1.SelectionCharOffset = -10
            End If

    However, the offset of the text becomes messed up when the font is increased, and the font size in the superscript/subscript stay at a small font size.

     

    Tuesday, October 1, 2013 2:24 AM

Answers

  • However, the offset of the text becomes messed up when the font is increased, and the font size in the superscript/subscript stay at a small font size.

    You cannot hard code these values into your calculations and expect them to work properly with different sized fonts - they must be based on the font size of the control.  This code corrects the calculations.  I have included your existing test for the current state of the super/subscript setting, but you have not provided information on what this is for, so it doesn't work properly in this example.  Perhaps it was just a carry over from some earlier example, and needs to be removed.

    Option Strict On
    Public Class Form1
    
        Private UpShift As Integer
        Private DownShift As Integer
        Private SSFontSize As Integer
        Private isSuperscripted As Boolean = False
        Private isSubScripted As Boolean = False
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            SetShiftSize()
        End Sub
        Private Sub btnSuper_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSuper.Click
            If Not isSuperscripted Then
                'Turn Superscipt On
                RichTextBox1.SelectionCharOffset = UpShift
                RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont.FontFamily, SSFontSize, RichTextBox1.SelectionFont.Style)
                isSuperscripted = True
            Else
                'Turn SuperScript Off
                RichTextBox1.SelectionCharOffset = 0
                RichTextBox1.SelectionFont = RichTextBox1.Font
                isSuperscripted = False
            End If
        End Sub
    
        Private Sub btnSub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSub.Click
            If Not isSubScripted Then
                'Turn Subscript On
                RichTextBox1.SelectionCharOffset = -DownShift
                RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont.FontFamily, SSFontSize, RichTextBox1.SelectionFont.Style)
                isSubScripted = True
            Else
                'Turn SubScript Off
                RichTextBox1.SelectionCharOffset = 0
                RichTextBox1.SelectionFont = RichTextBox1.Font
                isSubScripted = False
            End If
        End Sub
    
        Private Sub btnFontChooser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFontChooser.Click
            Dim FD As New FontDialog
            FD.Font = RichTextBox1.Font
            If FD.ShowDialog() <> DialogResult.Cancel Then
                RichTextBox1.Font = FD.Font
                SetShiftSize()
            End If
        End Sub
    
        Private Sub SetShiftSize()
            Dim g As Graphics = RichTextBox1.CreateGraphics
            Dim MS As SizeF = g.MeasureString("X", RichTextBox1.Font)
            UpShift = CInt(MS.Height * 0.2)
            DownShift = CInt(MS.Height * 0.3)
            SSFontSize = CInt(RichTextBox1.Font.Size * 0.75)
        End Sub
    
    End Class
    Tuesday, October 1, 2013 3:22 AM
  • It is bizarre, but like some code gets underlined like:

    Private Sub SpeakTheTextToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles SpeakTheTextToolStripMenuItem1.Click
            Dim Speech
            Speech = CreateObject("sapi.spvoice")
            Speech.speak(RichTextBox1.Text)
        End Sub
    and there was nothing wrong with it before... I tried deleting this code and then more code became underlined that was completely unrelated. I would have to post the whole class of code to show you the code that becomes underlined... It all eventually "collapses in on itself"

    It's probably due to this line:
       Option Strict On

    The message would be something like "Option Strict On prevents conversion from <something> to object".  You should not be declaring a variable such as Speech as an Object - it should be declared as the Type that you intend to use it as - the <something> in this case.  Declaring it properly will remove the error.

    Thursday, October 3, 2013 1:56 AM

All replies

  • @when the font is increased

    What does this mean?


    Click For donating:Free Rice For the poor
    For spamming-sender issues, you can either report it at Microsoft Spamming Issue, or just find "Report Spam Here+Number" at Forum Issue;You can also find "Verify Your Account+Number" at "Forum Issue", where you can submit to be confirmed to paste links or images.
    For more things to talk about? StackOverFlow is your choice.

    Tuesday, October 1, 2013 2:38 AM
  • However, the offset of the text becomes messed up when the font is increased, and the font size in the superscript/subscript stay at a small font size.

    You cannot hard code these values into your calculations and expect them to work properly with different sized fonts - they must be based on the font size of the control.  This code corrects the calculations.  I have included your existing test for the current state of the super/subscript setting, but you have not provided information on what this is for, so it doesn't work properly in this example.  Perhaps it was just a carry over from some earlier example, and needs to be removed.

    Option Strict On
    Public Class Form1
    
        Private UpShift As Integer
        Private DownShift As Integer
        Private SSFontSize As Integer
        Private isSuperscripted As Boolean = False
        Private isSubScripted As Boolean = False
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            SetShiftSize()
        End Sub
        Private Sub btnSuper_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSuper.Click
            If Not isSuperscripted Then
                'Turn Superscipt On
                RichTextBox1.SelectionCharOffset = UpShift
                RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont.FontFamily, SSFontSize, RichTextBox1.SelectionFont.Style)
                isSuperscripted = True
            Else
                'Turn SuperScript Off
                RichTextBox1.SelectionCharOffset = 0
                RichTextBox1.SelectionFont = RichTextBox1.Font
                isSuperscripted = False
            End If
        End Sub
    
        Private Sub btnSub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSub.Click
            If Not isSubScripted Then
                'Turn Subscript On
                RichTextBox1.SelectionCharOffset = -DownShift
                RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont.FontFamily, SSFontSize, RichTextBox1.SelectionFont.Style)
                isSubScripted = True
            Else
                'Turn SubScript Off
                RichTextBox1.SelectionCharOffset = 0
                RichTextBox1.SelectionFont = RichTextBox1.Font
                isSubScripted = False
            End If
        End Sub
    
        Private Sub btnFontChooser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFontChooser.Click
            Dim FD As New FontDialog
            FD.Font = RichTextBox1.Font
            If FD.ShowDialog() <> DialogResult.Cancel Then
                RichTextBox1.Font = FD.Font
                SetShiftSize()
            End If
        End Sub
    
        Private Sub SetShiftSize()
            Dim g As Graphics = RichTextBox1.CreateGraphics
            Dim MS As SizeF = g.MeasureString("X", RichTextBox1.Font)
            UpShift = CInt(MS.Height * 0.2)
            DownShift = CInt(MS.Height * 0.3)
            SSFontSize = CInt(RichTextBox1.Font.Size * 0.75)
        End Sub
    
    End Class
    Tuesday, October 1, 2013 3:22 AM
  • @when the font is increased
    What does this mean?

    It means 'when a larger font size is used'.

    Tuesday, October 1, 2013 4:38 AM
  • So I have a few problems with this.

    1. When this code was inserted, a bunch of code that already worked became underlined (which caused debugging errors)

    2. The font can be changed with a dialog box, and I also have preset font sizes:

     Private Sub PointSizeToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles PointSizeToolStripMenuItem.Click
            RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont.FontFamily, 12, RichTextBox1.SelectionFont.Style)
     End Sub

    Thanks.

    Wednesday, October 2, 2013 1:03 AM
  • 1. When this code was inserted, a bunch of code that already worked became underlined (which caused debugging errors)

    If you want assistance with the code that now has errors you would need to post that code and the associated error messages.

    2. The font can be changed with a dialog box, and I also have preset font sizes:

     Private Sub PointSizeToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles PointSizeToolStripMenuItem.Click
            RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont.FontFamily, 12, RichTextBox1.SelectionFont.Style)
     End Sub

    You haven't indicated what the problem is with the preset font sizes. Are you saying that the superscript and subscript offsets are wrong when you change to a preset size?  That would indicate a problem with calculating the offsets for that new size, so you would need to show the changes you made from the code I posted.

    • Edited by Acamar Wednesday, October 2, 2013 1:55 AM frmt
    Wednesday, October 2, 2013 1:54 AM
  • It is bizarre, but like some code gets underlined like:

    Private Sub SpeakTheTextToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles SpeakTheTextToolStripMenuItem1.Click
            Dim Speech
            Speech = CreateObject("sapi.spvoice")
            Speech.speak(RichTextBox1.Text)
        End Sub
    and there was nothing wrong with it before... I tried deleting this code and then more code became underlined that was completely unrelated. I would have to post the whole class of code to show you the code that becomes underlined... It all eventually "collapses in on itself"

    Thursday, October 3, 2013 1:23 AM
  • It is bizarre, but like some code gets underlined like:

    Private Sub SpeakTheTextToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles SpeakTheTextToolStripMenuItem1.Click
            Dim Speech
            Speech = CreateObject("sapi.spvoice")
            Speech.speak(RichTextBox1.Text)
        End Sub
    and there was nothing wrong with it before... I tried deleting this code and then more code became underlined that was completely unrelated. I would have to post the whole class of code to show you the code that becomes underlined... It all eventually "collapses in on itself"

    It's probably due to this line:
       Option Strict On

    The message would be something like "Option Strict On prevents conversion from <something> to object".  You should not be declaring a variable such as Speech as an Object - it should be declared as the Type that you intend to use it as - the <something> in this case.  Declaring it properly will remove the error.

    Thursday, October 3, 2013 1:56 AM