Can anybody help me for creating subscript and superscript in Richtextbox in c#. I am done in vb.net but could not convert it into c#.


here is my vb.net code...

Public Sub SetSubScript(ByRef rtb As System.Windows.Forms.RichTextBox)
        Dim iPos As Integer
        Dim strRTF As String

        With rtb

            If VB6.PixelsToTwipsX(.SelectionCharOffset) >= 0 Then

                iPos = .SelectionStart

                .SelectedText = Chr(128) & .SelectedText & Chr(129) ' \'80 \'81

                strRTF = Replace(.Rtf, "\'80", "\sub\dn1 ")

                .Rtf = Replace(strRTF, "\'81", "\nosupersub\up0 ")
                'MessageBox .Show 
                .SelectionStart = iPos


            Else

                .SelectedText = Chr(128) & .SelectedText

                strRTF = .Rtf

                .Rtf = Replace(strRTF, "\'80", "\nosupersub\up0 ", , 1)

            End If

        End With
        'rtb.Select()

    End Sub

Have you tried to convert this code to c#?

hey abelLazm thanks for your reply but this link is only display the Richtextbox property.

Check this code and follow this link for details

private void button4_Click(object sender, EventArgs e)
        {
            // Clear all text from the RichTextBox.
            richTextBox1.Clear();
            // Set the font for the text.
            richTextBox1.SelectionFont = new Font("Lucinda Console", 12);
            // Set the foreground color of the text.
            richTextBox1.SelectionColor = Color.Purple;
            // Set the baseline text.
            richTextBox1.SelectedText = "Check this code and follow";
            // Set the CharOffset to display superscript text.
            richTextBox1.SelectionCharOffset = 10;
            // Set the superscripted text.	
            richTextBox1.SelectedText = "2";
            // Reset the CharOffset to display text at the baseline.
            richTextBox1.SelectionCharOffset = 0;
            richTextBox1.AppendText("\n\n");
            // Change the forecolor of the next text selection.
            richTextBox1.SelectionColor = Color.Blue;
            // Set the baseline text.
            richTextBox1.SelectedText = "Check this code and follow";
            // Set the CharOffset to display subscript text.
            richTextBox1.SelectionCharOffset = -10;
            // Set the subscripted text.  
            richTextBox1.SelectedText = "3";
            // Reset the CharOffset to display text at the baseline.
            richTextBox1.SelectionCharOffset = 0; 

        }

Hey thanks for your reply
I solve this problem using SelectionCharOffset property ot richtextbox,
Here is code for superscript

rtxtmaterial.SelectionCharOffset = 10;
            rtxtmaterial.SelectedText = rtxtmaterial.SelectedText;
            rtxtmaterial.SelectionCharOffset = 0;
            rtxtmaterial.AppendText("\n\n");
            rtxtmaterial.SelectionCharOffset = -10;
            rtxtmaterial.SelectionCharOffset = 0;

and for subscript

rtxtmaterial.SelectionCharOffset = -10;
            rtxtmaterial.SelectedText = rtxtmaterial.SelectedText;
            rtxtmaterial.SelectionCharOffset = 0;
            rtxtmaterial.AppendText("\n\n");               
            rtxtmaterial.SelectionCharOffset = 10;          
            rtxtmaterial.SelectionCharOffset = 0;

Please help

How can change fore color(text color) of richtextbox in vb6.0

Ramesh Genwa

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.