如何在Visual C#中显示下标?

时间:2012-12-20 23:02:34

标签: c# subscript

我正在为我的A level Computing项目制作一个射弹运动模拟程序,我需要使用物理符号和下标。我不知道如何在标签或富文本框中使用它。 有人可以给我帮助/代码来实现这个吗?

2 个答案:

答案 0 :(得分:2)

您需要使用SelectionCharOffset中的RichTextBox属性来完成此操作。

对于下标,将数字设为负数,如下所示:

richTextBox1.SelectionCharOffset = -10;

答案 1 :(得分:0)

SelectioncharOffset不是您的最佳选择。最好是操纵rtf并添加下标标签。

//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;