日期:2014-05-18 浏览次数:20799 次
private static int i; private void richTextBox2_TextChanged(object sender, EventArgs e) { j=richTextBox1.Text.Length; while(i<j) { if (richTextBox1.Text[i]!= richTextBox2.Text[i]) { richTextBox2.Select(i, 1); richTextBox2.SelectionColor = Color.Red; } i++; }
private void richTextBox2_TextChanged(object sender, EventArgs e) { int start = richTextBox2.SelectionStart; int m = richTextBox1.TextLength > richTextBox2.TextLength ? richTextBox2.TextLength : richTextBox1.TextLength; for (int i = 0; i < m; i++) { if (richTextBox1.Text[i] != richTextBox2.Text[i]) { richTextBox2.Select(i, 1); richTextBox2.SelectionColor = Color.Red; } } if (richTextBox2.TextLength > richTextBox1.TextLength) { richTextBox2.Select(richTextBox1.TextLength, richTextBox2.TextLength - richTextBox1.TextLength); richTextBox2.SelectionColor = Color.Red; } richTextBox2.SelectionStart = start; richTextBox2.SelectionColor = richTextBox1.ForeColor; }
------解决方案--------------------
private void richTextBox2_TextChanged(object sender, EventArgs e) { Int32 iLen = richTextBox2.Text.Length; if (iLen > 0 && iLen < richTextBox1.Text.Length) { if (richTextBox1.Text[iLen-1] != richTextBox2.Text[iLen-1]) { richTextBox2.Select(iLen-1, 1); richTextBox2.SelectionColor = Color.Red; richTextBox2.SelectionLength = 0; richTextBox2.SelectionStart = iLen; richTextBox2.SelectionColor = Color.Black; } } }