0

I am making a projectile motion simulation program for my A level Computing project and I need to use physics notation with subscripts. I have no idea how to use this in a label or a rich text box. Can anyone please give me help/code to implement this?

user1920206
  • 69
  • 10
  • Welcome to StackOverflow :) Since you're new, and your first posting, it is prudent to understand how SO works. Show us some effort on your part in understanding your problem. :) What have you tried? Researched? Does your university/college not show you how to perform research? Look, this is not coming across as rude, but seeing the punchline "*Can anyone please give me help/code to implement this?*" indicates zero effort, i.e. laziness... :) – t0mm13b Dec 20 '12 at 23:28

2 Answers2

2

You need to use the SelectionCharOffset property in a RichTextBox to accomplish this.

For subscript, make the number negative, like this:

richTextBox1.SelectionCharOffset = -10;
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • That's a superscript. Use a positive number for the `SelectionCharOffset`. – Robert Harvey Dec 20 '12 at 23:19
  • The V bit is normal, the vertical bit is subscript. Is there a way to select parts which are subscripted? – user1920206 Dec 20 '12 at 23:21
  • 1
    Read the [docs](http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectioncharoffset.aspx), there's a detailed example there. If you are the one who subscripted the text, you should already have it selected. – Robert Harvey Dec 20 '12 at 23:23
0

SelectioncharOffset is not your best option. Is is better to manipulate the rtf and add the subscript labels.

//This allows you to reselect the text in the rtb after formatting
            int SelectionStart = richTextBox1.SelectionStart;
            int SelectionLength = richTextBox1.SelectionLength;

            string selectedRtf = richTextBox1.SelectedRtf;
            int Start;
            int End;
            string pre;
            string Mid;
            string post;

            //remove superscript from selected text
            Start = selectedRtf.IndexOf("\\super");
            while (Start != -1)
            {
                pre = selectedRtf.Substring(0, Start);
                post = selectedRtf.Substring(Start + 6, selectedRtf.Length - (Start + 6));
                selectedRtf = pre.Trim() + post.Trim();
                Start = selectedRtf.IndexOf("\\super");
            }

            //if selected text does not contain subscript
            if (selectedRtf.IndexOf("\\sub") == -1 && selectedRtf.IndexOf("\\nosupersub") == -1)
            {
                Start = selectedRtf.IndexOf("}}") + 2;
                End = selectedRtf.IndexOf("}", Start);
                pre = selectedRtf.Substring(0, Start);
                Mid = selectedRtf.Substring(Start, End - Start);
                post = selectedRtf.Substring(End, selectedRtf.Length - End);
                selectedRtf = pre + "\\sub" + Mid + "\\nosupersub" + post;
                goto Clean;
            }
            //if selected text contains subscript
            if (selectedRtf.IndexOf("\\sub") > 0)
            {
                Start = selectedRtf.IndexOf("\\sub");
                while (Start != -1)
                {
                    pre = selectedRtf.Substring(0, Start);
                    post = selectedRtf.Substring(Start + 4, selectedRtf.Length - (Start + 4));
                    selectedRtf = pre.Trim() + post.Trim();
                    Start = selectedRtf.IndexOf("\\sub");

                }
                goto Clean;
            }


        Clean:
            richTextBox1.SelectedRtf = selectedRtf;
            richTextBox1.Focus();
            richTextBox1.SelectionStart = SelectionStart;
            richTextBox1.SelectionLength = SelectionLength;