2014-03-19 126 views
2

我想選擇位於richtextbox文本的最後一個「{」和「}」之間的文本。 我有下一個代碼,但我有一個「LastIndexOf」函數的錯誤,我不知道如何解決它。有人能給我一些幫助嗎?從C#中的Richtextbox中選擇文本

private void highlightText() 
    { 
     mRtbxOperations.SelectionStart = mRtbxOperations.Text.LastIndexOf(@"{", 1, mRtbxOperations.SelectionStart); 
     mRtbxOperations.SelectionLength = mRtbxOperations.Text.IndexOf(@"}", mRtbxOperations.SelectionStart, mRtbxOperations.Text.Length - 1); 
     mRtbxOperations.SelectionBackColor = Color.LightBlue; 
     mRtbxOperations.SelectionFont = new Font(mRtbxOperations.SelectionFont, FontStyle.Underline); 
     mRtbxOperations.SelectionLength = 0; 
    } 

LastIndexOf錯誤:

The count must be positive and must refer to a location within the string, array or collection. Parameter name: count

+0

可能重複(http://stackoverflow.com/questions/455713/selectively-coloring-text-in-richtextbox) –

回答

1

您LastIndexOf參數混亂,以及選擇的長度,你需要。減去的出發點,以獲得適當的長度:

如下我將重寫代碼。

嘗試使用簡單的版本:

int textStart = mRtbxOperations.Text.LastIndexOf(@"{", 
               mRtbxOperations.SelectionStart); 
if (textStart > -1) { 
    int textEnd = mRtbxOperations.Text.IndexOf(@"}", textStart); 
    if (textEnd > -1) { 
    mRtbxOperations.Select(textStart, textEnd - textStart + 1); 
    mRtbxOperations.SelectionBackColor = Color.LightBlue; 
    } 
} 
的[RichTextBox中選擇性着色文本]
+0

這個工程,但我想接收lastIndexOf我的'{'附近我的鼠標點擊文本。我的意思是...我點擊了我的richedTextBox,並且想要選擇距離我的鼠標點擊最近的「{」和「}」的文本部分,因此我需要執行「mRtbOperations.Text.LastIndexOf(@」{「, 0,/ * mouseClickPosition * /);「它再次出現同樣的錯誤。 – Imrik

+0

@Imrik然後我不認爲你想要LastIndexOf。嘗試'int textStart = mRtbxOperations.Text.IndexOf(@「{」,mRtbxOperations.SelectionStart);' – LarsTech

+0

我需要LastIndexOf,因爲我的文本是,例如:「if(valueGenMaxProp!= 0){myPaint();} if firstLap == true){pbxTriangle.Location = new Point(200,100);}「我需要爲第一個或第二個段加下劃線,它取決於mouseClick的位置,即」mRtbxOperations.SelectionStart「。謝謝! – Imrik

1

看來你要出來的文字界。當你得到一個子字符串或一個索引時,你總是應該使用字符串邊界或者子字符串邊界。此外,您需要檢查選擇是否有效。

private void highlightText() 
    { 
     Selection selection = GetSelection(mRtbxOperations.Text); 
     if (selection == null) 
      return; 
     mRtbxOperations.SelectionStart = selection.Start; 
     mRtbxOperations.SelectionLength = selection.Length; 
     mRtbxOperations.SelectionBackColor = Color.LightBlue; 
     mRtbxOperations.SelectionFont = new Font(mRtbxOperations.SelectionFont, 
      FontStyle.Underline); 
    } 

    private static Selection GetSelection(string text) 
    { 
     int sIndex = text.LastIndexOf(@"{"); 
     if (sIndex == -1) 
      return null; 
     int eIndex = text.IndexOf(@"}", sIndex); 
     if (eIndex == -1) 
      return null; 

     return new Selection(sIndex + 1, eIndex); 
    } 

    public class Selection 
    { 
     public int Start { get; set; } 
     public int End { get; set; } 

     public int Length 
     { 
      get 
      { 
       return End - Start; 
      } 
     } 

     public Selection(int startIndex, int endIndex) 
     { 
      this.Start = startIndex; 
      this.End = endIndex; 
     } 
    } 
+0

我在另一個解決方案中有同樣的問題,但我不會接收lastIndexOf我的'{'在我的鼠標點擊文本附近。我的意思是...我點擊了我的richedTextBox,並且想要選擇距離我的鼠標點擊最近的「{」和「}」的文本部分,因此我需要執行「mRtbOperations.Text.LastIndexOf(@」{「, 0,/ * mouseClickPosition * /);「它再次出現同樣的錯誤。謝謝! – Imrik

+0

好的。我明白。使用LastIndexOf查看我的編輯。它應該工作。 –

+0

它也行得通,謝謝! :) – Imrik

相關問題