2012-12-20 103 views
0

我正在爲我的A級計算項目製作一個彈丸運動模擬程序,我需要使用帶有下標的物理符號。我不知道如何在標籤或富文本框中使用它。 任何人都可以請給我幫助/代碼來實現這個?如何在Visual C#中顯示下標?

+0

歡迎來到StackOverflow :)由於您是新手,並且您首次發帖,因此瞭解SO的工作原理是謹慎的。向我們表明您在理解您的問題方面所做的一些努力。 :)你有什麼嘗試?研究?你的大學/學院是否不告訴你如何進行研究?看,這不是粗魯的,但看到這個問題「*任何人都可以請給我幫助/代碼來實現這個?*」表示零努力,即懶惰...... :) – t0mm13b

回答

2

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

對標,使號碼負,這樣的:

richTextBox1.SelectionCharOffset = -10; 
+0

謝謝。我將如何顯示這樣的內容:http://i.imgur.com/B6xqx.png – user1920206

+0

這是一個上標。爲'SelectionCharOffset'使用一個正數。 –

+0

V位正常,垂直位爲下標。有沒有辦法選擇下標的零件? – user1920206

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;