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

Dani AI

Generated

Nice catch by : in WinForms the simplest route is the selection’s vertical offset. Since already proved it works, here is a reusable toggle approach you can drop into your toolbar handlers. It applies to the current selection and lets you quickly clear back to baseline.

// WinForms: toggle helpers
static void ToggleSuperscript(RichTextBox rtb, int offset = 8)
{
    if (rtb == null || rtb.SelectionLength == 0) return;
    rtb.SelectionCharOffset = (rtb.SelectionCharOffset > 0) ? 0 : Math.Abs(offset);
}

static void ToggleSubscript(RichTextBox rtb, int offset = 8)
{
    if (rtb == null || rtb.SelectionLength == 0) return;
    rtb.SelectionCharOffset = (rtb.SelectionCharOffset < 0) ? 0 : -Math.Abs(offset);
}

// Tip: when saving/loading, keep formatting by using RTF.
string selectedRtf = richTextBox1.SelectedRtf;   // persist this
// ... later ...
richTextBox1.SelectedRtf = selectedRtf;          // restores superscripts/subscripts

Working in WPF instead of WinForms? Use baseline alignment on the selection:

var sel = richTextBox.Selection;
sel.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Superscript);
// or:
sel.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Subscript);
// clear:
sel.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Baseline);

If the tag really meant ASP.NET: you will not have a WinForms RichTextBox. Use an HTML editor and emit semantic markup, e.g. H<sub>2</sub>O, 10<sup>th</sup>.

And for (VB6.0 RichTextBox forecolor): set the color on the current selection.

' VB6
RichTextBox1.SelStart = 0
RichTextBox1.SelLength = Len(RichTextBox1.Text)
RichTextBox1.SelColor = RGB(255, 0, 0)  ' or a VB color constant like vbBlue

Recommended Answers

All 7 Replies

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.