none
Superscript in a rich text box

    Question

  • Hi friends,

    i'm currently working on a sample program for differentiation. And i'm having a problem with superscript text in a rich text box. Is there a way to check whether the text has  superscript numbers? ,and if then how can i get those superscript text as a string variable ?

    Wednesday, August 27, 2014 4:49 AM

Answers

  • This code in Visual Basic will test it. But by letter. For this the RTF for each super or sub scripted character has the word super or sub for that particular character. So for a word I suppose characters with the word super that don't have a space between them would be a word. Although one word in the below text has both super and sub scripted characters in it (WordPad).

    Also I tried using the RichTextBox.SelectionCharOffset to see if it would get the selected texts selection char offset but it didn't do it and I  was unable to find any code where it was used to get rather than set selected text to superscript or subscript or baseline.

    For the test I used WordPad to create an RTF file for loading into the RichTextBox. And super/sub scripted some text in the WordPad document.

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
            RichTextBox1.Rtf = My.Computer.FileSystem.ReadAllText("C:\Users\John\Desktop\RTBDoc.Rtf")
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For i = 0 To RichTextBox1.Text.Length - 2
                RichTextBox1.Focus()
                RichTextBox1.SelectionStart = i
                RichTextBox1.Select(i, 1)
                If RichTextBox1.SelectedRtf.Contains("\sub\") Then
                    RichTextBox2.AppendText(i.ToString & " subscript = " & RichTextBox1.SelectedText & vbCrLf)
                ElseIf RichTextBox1.SelectedRtf.Contains("\super\") Then
                    RichTextBox2.AppendText(i.ToString & " superscript = " & RichTextBox1.SelectedText & vbCrLf)
                End If
            Next
        End Sub
    
    End Class


    La vida loca


    Thursday, August 28, 2014 5:57 AM
  • hi Mr.Monkeyboy

    Thank you very much for your reply and it was successful.

    but i have another question ,    There's a textbox and the user can enter any function in it, if there's a function like this  " 2x²+5x³  " , and i will load it in to a string variable. After that i want to split this string from the "+" mark into 2 substrings. How can i do it? .  i did some coding with  "string.split " code  , but not succeeded.

    thank you ,

    Nuwan 


    Really that's a second question. So you should create a new question thread for that. But if this works for you then don't.

    Label1's text is a string. SplitString is a string array. Splitting Label1's text at the plus "+" will make two strings in this instance as there is only one plus in the string being split. Note the actual plus sign is nowhere to be found in SplitString after the split occurs.

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
            Label1.Text = " 2x²+5x³ "
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim SplitString() As String
            SplitString = Label1.Text.Split("+"c)
            Label2.Text = "First half is " & SplitString(0) & " and the second half is " & SplitString(1) & "."
        End Sub
    
    End Class


    La vida loca

    Thursday, August 28, 2014 3:37 PM

All replies

  • HI,

    you can save superscript text in text box as below. Also you can use Unicode for that.

    var o2 = "O₂"; // or "O\x2082" 
    var unit2 = "unit²"; // or "unit\xB2"

    Thank You,

    Sammani

    http://sammanipalansuriya.blogspot.com/

    Wednesday, August 27, 2014 10:28 AM
  • Hi there,

    thank you very much for your reply.

    But actually i want to check to see whether there are superscript text in the text which user has entered in the rich text box. Is there anyway to do that? either  in vb.net or in C#

    Thank you .


    Thursday, August 28, 2014 4:02 AM
  • This code in Visual Basic will test it. But by letter. For this the RTF for each super or sub scripted character has the word super or sub for that particular character. So for a word I suppose characters with the word super that don't have a space between them would be a word. Although one word in the below text has both super and sub scripted characters in it (WordPad).

    Also I tried using the RichTextBox.SelectionCharOffset to see if it would get the selected texts selection char offset but it didn't do it and I  was unable to find any code where it was used to get rather than set selected text to superscript or subscript or baseline.

    For the test I used WordPad to create an RTF file for loading into the RichTextBox. And super/sub scripted some text in the WordPad document.

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
            RichTextBox1.Rtf = My.Computer.FileSystem.ReadAllText("C:\Users\John\Desktop\RTBDoc.Rtf")
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For i = 0 To RichTextBox1.Text.Length - 2
                RichTextBox1.Focus()
                RichTextBox1.SelectionStart = i
                RichTextBox1.Select(i, 1)
                If RichTextBox1.SelectedRtf.Contains("\sub\") Then
                    RichTextBox2.AppendText(i.ToString & " subscript = " & RichTextBox1.SelectedText & vbCrLf)
                ElseIf RichTextBox1.SelectedRtf.Contains("\super\") Then
                    RichTextBox2.AppendText(i.ToString & " superscript = " & RichTextBox1.SelectedText & vbCrLf)
                End If
            Next
        End Sub
    
    End Class


    La vida loca


    Thursday, August 28, 2014 5:57 AM
  • hi Mr.Monkeyboy

    Thank you very much for your reply and it was successful.

    but i have another question ,    There's a textbox and the user can enter any function in it, if there's a function like this  " 2x²+5x³  " , and i will load it in to a string variable. After that i want to split this string from the "+" mark into 2 substrings. How can i do it? .  i did some coding with  "string.split " code  , but not succeeded.

    thank you ,

    Nuwan 


    Thursday, August 28, 2014 1:35 PM
  • hi Mr.Monkeyboy

    Thank you very much for your reply and it was successful.

    but i have another question ,    There's a textbox and the user can enter any function in it, if there's a function like this  " 2x²+5x³  " , and i will load it in to a string variable. After that i want to split this string from the "+" mark into 2 substrings. How can i do it? .  i did some coding with  "string.split " code  , but not succeeded.

    thank you ,

    Nuwan 


    Really that's a second question. So you should create a new question thread for that. But if this works for you then don't.

    Label1's text is a string. SplitString is a string array. Splitting Label1's text at the plus "+" will make two strings in this instance as there is only one plus in the string being split. Note the actual plus sign is nowhere to be found in SplitString after the split occurs.

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
            Label1.Text = " 2x²+5x³ "
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim SplitString() As String
            SplitString = Label1.Text.Split("+"c)
            Label2.Text = "First half is " & SplitString(0) & " and the second half is " & SplitString(1) & "."
        End Sub
    
    End Class


    La vida loca

    Thursday, August 28, 2014 3:37 PM