using
namespace
delegate string GetTextCallback();delegate void SelectionStartCallBack(int start);delegate void SelectionLengthCallBack(int Length);delegate void SelectionColorCallBack(Color c);
ThreadStart tStart;Thread FormatThread;
public Form1(){InitializeComponent();tStart = new ThreadStart(FormatIDE);FormatThread = new Thread(tStart);openFileDialog1.ShowDialog();
using (StreamReader sr = new StreamReader(openFileDialog1.FileName)){string ch;while ((ch = sr.ReadLine()) != null){richTextBoxTPG.Text = richTextBoxTPG.Text + ch + "\n";}sr.Close();}}private string GetText(){return richTextBoxTPG.Text;}
private void SelectionStart(int start){richTextBoxIDE.SelectionStart = start;}
private void SelectionLength(int Length){richTextBoxIDE.SelectionLength = Length;}
private void SelectionColor(Color c){richTextBoxIDE.SelectionColor = c;}
private void FormatIDE(){CharEnumerator ce;string temp;
GetTextCallback d = new GetTextCallback(GetText);SelectionColorCallBack sc = new SelectionColorCallBack(SelectionColor);SelectionLengthCallBack sl = new SelectionLengthCallBack(SelectionLength);SelectionStartCallBack ss = new SelectionStartCallBack(SelectionStart);if (this.richTextBoxIDE.InvokeRequired){temp = richTextBoxIDE.Invoke(d).ToString();ce = temp.GetEnumerator();}else{ce = richTextBoxIDE.Text.GetEnumerator();temp = richTextBoxIDE.Text;}char eachChar;int count = 0;int commentCount = 0, commentStart = 0;while (count<temp.Length){eachChar = Convert.ToChar(temp[count]);count++;if (count<temp.Length){if ((eachChar == '/') && (Convert.ToChar(temp[count]) == '*')){if (commentCount == 0)commentStart = count - 1;commentCount++;}if ((eachChar == '*') && (Convert.ToChar(temp[count]) == '/')){commentCount--;if (commentCount < 0){//Inform the user with mismatched comment.commentCount = 0;}if (commentCount == 0){if (this.richTextBoxIDE.InvokeRequired){richTextBoxIDE.Invoke(ss, commentStart);richTextBoxTPG.Invoke(sl, count - commentStart + 1);richTextBoxTPG.Invoke(sc, Color.Green);}else{richTextBoxIDE.SelectionStart = commentStart;richTextBoxIDE.SelectionLength = count - commentStart + 1;richTextBoxIDE.SelectionColor = Color.Green;}}}} }if (this.richTextBoxIDE.InvokeRequired){richTextBoxIDE.Invoke(ss, temp.Length);}else{richTextBoxIDE.SelectionStart = temp.Length;}}
private void richTextBoxTPG_KeyUp(object sender, KeyEventArgs e){if (!FormatThread.IsAlive && e.KeyCode != Keys.Up && e.KeyCode != Keys.Down && e.KeyCode != Keys.Right && e.KeyCode != Keys.Left){FormatThread.Start();FormatThread = new Thread(tStart);}}
}