Я делаю Блокнот с подсветкой синтаксиса.
Я сделал подсветку синтаксиса, но теперь мне нужна помощь: D
Мне нужна «Найти форму», которая работает с этимкод:
// getting keywords/functions
string keywords = @"\b(abstract|as|base|break|case|catch|checked|continue|default|delegate|do|else|event|explicit|extern|false|finally|fixed|for|foreach|goto|if|implicit|in|interface|internal|is|lock|namespace|new|null|object|operator|out|override|params|private|protected|public|readonly|ref|return|sealed|sizeof|stackalloc|switch|this|throw|true|try|typeof|unchecked|unsafe|using|virtual|volatile|while)\b";
MatchCollection keywordMatches = Regex.Matches(codeRichTextBox.Text, keywords);
string purplewords = @"\b(bool|byte|char|class|const|decimal|double|enum|float|int|long|sbyte|short|static|string|struct|uint|ulong|ushort|static|void)\b";
MatchCollection purplewordsMatches = Regex.Matches(codeRichTextBox.Text, purplewords);
// getting types/classes from the text
string types = @"\b(Console)\b";
MatchCollection typeMatches = Regex.Matches(codeRichTextBox.Text, types);
// getting comments (inline or multiline)
string comments = @"(\/\/.+?$|\/\*.+?\*\/)";
MatchCollection commentMatches = Regex.Matches(codeRichTextBox.Text, comments, RegexOptions.Multiline);
// getting strings
string strings = "\".+?\"";
MatchCollection stringMatches = Regex.Matches(codeRichTextBox.Text, strings);
// saving the original caret position + forecolor
int originalIndex = codeRichTextBox.SelectionStart;
int originalLength = codeRichTextBox.SelectionLength;
Color originalColor = Color.Black;
// MANDATORY - focuses a label before highlighting (avoids blinking)
menuStrip1.Focus();
// removes any previous highlighting (so modified words won't remain highlighted)
codeRichTextBox.SelectionStart = 0;
codeRichTextBox.SelectionLength = codeRichTextBox.Text.Length;
codeRichTextBox.SelectionColor = originalColor;
// scanning...
foreach (Match m in keywordMatches)
{
codeRichTextBox.SelectionStart = m.Index;
codeRichTextBox.SelectionLength = m.Length;
codeRichTextBox.SelectionColor = Color.Blue;
}
foreach (Match m in purplewordsMatches)
{
codeRichTextBox.SelectionStart = m.Index;
codeRichTextBox.SelectionLength = m.Length;
codeRichTextBox.SelectionColor = Color.Purple;
}
foreach (Match m in typeMatches)
{
codeRichTextBox.SelectionStart = m.Index;
codeRichTextBox.SelectionLength = m.Length;
codeRichTextBox.SelectionColor = Color.DarkCyan;
}
foreach (Match m in commentMatches)
{
codeRichTextBox.SelectionStart = m.Index;
codeRichTextBox.SelectionLength = m.Length;
codeRichTextBox.SelectionColor = Color.Green;
}
foreach (Match m in stringMatches)
{
codeRichTextBox.SelectionStart = m.Index;
codeRichTextBox.SelectionLength = m.Length;
codeRichTextBox.SelectionColor = Color.Brown;
}
// restoring the original colors, for further writing
codeRichTextBox.SelectionStart = originalIndex;
codeRichTextBox.SelectionLength = originalLength;
codeRichTextBox.SelectionColor = originalColor;
// giving back the focus
codeRichTextBox.Focus();
- То есть Полная C # подсветка. Мне нужно, чтобы он работал с «Find Form», как в Notepad ++:)
Мой текущий код «Find Form»:
public static void Find(RichTextBox rtb, String word, Color color)
{
rtb.SelectionStart = 0;
rtb.SelectionLength = rtb.TextLength;
rtb.SelectionBackColor = Color.White;
if (word == "")
{
return;
}
int s_start = rtb.SelectionStart, startIndex = 0, index;
while ((index = rtb.Text.IndexOf(word, startIndex)) != -1)
{
rtb.Select(index, word.Length);
rtb.SelectionBackColor = color;
startIndex = index + word.Length;
}
}
Работает нормально (Без синтаксиса ), но если у меня включен синтаксис ON , он дает сбой: (* 1023 *
Я могу предоставить больше информации, если это необходимо:)
PS: я знаю, что задавал вопрос для «Найти форму» раньше, но это другой тип вопроса:)