2011-07-21 32 views
0

我已經寫了一個邏輯比較兩個ricttext箱內容,但差輸出文本沒有在預期的指數突出比較兩個RichTextBox的文本。它在預期指數之前開始。我在下面給出了我用過的邏輯。如何使用C#

private void CompareRichTextContents(RichTextBox target, RichTextBox dest) 
    { 
     string[] targetLines = target.Lines; 
     string[] destLine = dest.Lines; 
     List<string> result=targetLines.Except(destLine).ToList<string>(); 
     foreach (string str in result) 
     { 

      int lineIndex=Array.IndexOf(targetLines, str); 
      int lineCount=0; 
      for (int index = 0; index < lineIndex; index++) 
      { 
       lineCount+=targetLines[index].Length; 
      } 
      target.SelectionBackColor = Color.Yellow; 
      target.SelectionColor = Color.Black; 
      target.SelectionStart = lineCount; 
      target.SelectionLength = str.Length; 

     } 
    } 

謝謝, Lokesh。

+0

哪條線它凸顯,到底是什麼? – MGZero

+0

例如,我有下面線路目標部分: <尺寸REF = 「C2」/> DestPart 行應該從尺寸被突出顯示,但它也會從開始「/ >「來自上一行 – Lokesh

回答

0

找換行,可能是這個問題

+0

我希望richtext行中的所有內容都以字符串形式讀取,並且在將行轉換爲char數組時其長度相同。我不明白這部分中的換行符是什麼意思。 – Lokesh

+0

能否請您澄清 – Lokesh

+0

如果所以請讓我知道如何尋找換行符 – Lokesh

1

你的方法還好吧但是豐富的文本框只能進一步選擇一個時間,你必須增加1

做如下:

私人詮釋CompareRichTextContents(RichTextBox的目標,RichTextBox的目標寄存器) { INT計數器= 0; string [] targetLines = target.Lines; string [] destLine = dest.Lines; 列表結果= targetLines.Except(destLine).ToList(); 的foreach(在結果字符串str){

  int lineIndex = Array.IndexOf(targetLines, str); 
      int lineCount = 0; 
      for (int index = 0; index < lineIndex; index++) 
      { 
       lineCount += targetLines[index].Length+1; 
      } 
      target.SelectionBackColor = Color.Yellow; 
      target.SelectionColor = Color.Black; 
      // target.Select(lineCount, str.Length); 
      target.SelectionStart = lineCount; 
      target.SelectionLength = str.Length; 
      counter++; 

     } 
     return counter; 
    } 

現在在按鈕的單擊事件,稱之爲多次多項選擇。雖然這是在調用不必要的時間(寫作時沒有多少思考),但每次都會選擇下一個。

private void button1_Click(object sender, EventArgs e) 
     { 
      int counter= CompareRichTextContents(this.richTextBox1Body, this.richTextBox2Body); 
      for (int i = 0; i < counter; i++) 
       CompareRichTextContents(this.richTextBox1Body, this.richTextBox2Body); 
     }