private void copyToolStripButton1_Click(object sender, EventArgs e) { TBJoiner.SelectAll(); TBJoiner.Copy(); TBJoiner.Focus(); }
private void SqlColored( ) { // getting keywords/functions string keywords = in2SqlLibrary.getMsSqlReserved(); MatchCollection keywordMatches = Regex.Matches(TBJoiner.Text.ToUpper(), keywords); // getting types/classes from the text string types = @"\b(Console)\b"; MatchCollection typeMatches = Regex.Matches(TBJoiner.Text, types); // getting comments (inline or multiline) string comments = @"(\/\/.+?$|\/\*.+?\*\/)"; MatchCollection commentMatches = Regex.Matches(TBJoiner.Text, comments, RegexOptions.Multiline); // getting strings string strings = "\".+?\""; MatchCollection stringMatches = Regex.Matches(TBJoiner.Text, strings); // saving the original caret position + forecolor int originalIndex = TBJoiner.SelectionStart; int originalLength = TBJoiner.SelectionLength; Color originalColor = Color.Black; // MANDATORY - focuses a label before highlighting (avoids blinking) this.Focus(); // removes any previous highlighting (so modified words won't remain highlighted) TBJoiner.SelectionStart = 0; TBJoiner.SelectionLength = TBJoiner.Text.Length; TBJoiner.SelectionColor = originalColor; // scanning... foreach (Match m in keywordMatches) { TBJoiner.SelectionStart = m.Index; TBJoiner.SelectionLength = m.Length; TBJoiner.SelectionColor = Color.Blue; } foreach (Match m in typeMatches) { TBJoiner.SelectionStart = m.Index; TBJoiner.SelectionLength = m.Length; TBJoiner.SelectionColor = Color.DarkCyan; } foreach (Match m in stringMatches) { TBJoiner.SelectionStart = m.Index; TBJoiner.SelectionLength = m.Length; TBJoiner.SelectionColor = Color.Brown; } foreach (Match m in commentMatches) { TBJoiner.SelectionStart = m.Index; TBJoiner.SelectionLength = m.Length; TBJoiner.SelectionColor = Color.Green; } // restoring the original colors, for further writing TBJoiner.SelectionStart = originalIndex; TBJoiner.SelectionLength = originalLength; TBJoiner.SelectionColor = originalColor; // giving back the focus TBJoiner.Focus(); }