如何在保留 SelectionColor 的同时为 RichTextBox 创建 "Find Form"? C#
How can I make a "Find Form" for RichTextBox while keeping SelectionColor? C#
我正在制作一个带语法高亮的记事本。
我完成了语法高亮显示,但现在我需要帮助:D
我需要一个使用此代码的 "Find Form":
// 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();
- 即Full 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 , 它出现故障:(
如果需要,我可以提供更多信息:)
P.S:我知道我之前提过一个 "Find Form" 的问题,但这是一个不同类型的问题 :)
因此,如果您决定使用 Scintilla.NET,那么您希望直接从 visual studio (https://docs.microsoft.com/en-us/nuget/quickstart/install-and-use-a-package-in-visual-studio) 安装 nuget 包。然后您可以通过 using ScintillaNET;
和 new Scintilla()
.
创建一个新的
我已经在我的代码库 (C#) 中使用了它 https://github.com/HicServices/RDMP/blob/f85f1c7f03bc0cdcbabe7ef83d12fa1f4d25bdae/Reusable/ReusableUIComponents/ScintillaHelper/ScintillaTextEditorFactory.cs
这是摘录
var toReturn = new Scintilla();
toReturn.Dock = DockStyle.Fill;
toReturn.HScrollBar = true;
toReturn.VScrollBar = true;
if (lineNumbers)
toReturn.Margins[0].Width = 40; //allows display of line numbers
else
foreach (var margin in toReturn.Margins)
margin.Width = 0;
toReturn.ClearCmdKey(Keys.Control | Keys.S); //prevent Ctrl+S displaying ascii code
toReturn.ClearCmdKey(Keys.Control | Keys.R); //prevent Ctrl+R displaying ascii code
toReturn.ClearCmdKey(Keys.Control | Keys.W); //prevent Ctrl+W displaying ascii code
显示后,您可以在此处查看有关自动代码突出显示的文档 https://github.com/jacobslusser/ScintillaNET/wiki/Custom-Syntax-Highlighting
我正在制作一个带语法高亮的记事本。
我完成了语法高亮显示,但现在我需要帮助:D
我需要一个使用此代码的 "Find Form":
// 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();
- 即Full 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 , 它出现故障:(
如果需要,我可以提供更多信息:)
P.S:我知道我之前提过一个 "Find Form" 的问题,但这是一个不同类型的问题 :)
因此,如果您决定使用 Scintilla.NET,那么您希望直接从 visual studio (https://docs.microsoft.com/en-us/nuget/quickstart/install-and-use-a-package-in-visual-studio) 安装 nuget 包。然后您可以通过 using ScintillaNET;
和 new Scintilla()
.
我已经在我的代码库 (C#) 中使用了它 https://github.com/HicServices/RDMP/blob/f85f1c7f03bc0cdcbabe7ef83d12fa1f4d25bdae/Reusable/ReusableUIComponents/ScintillaHelper/ScintillaTextEditorFactory.cs
这是摘录
var toReturn = new Scintilla();
toReturn.Dock = DockStyle.Fill;
toReturn.HScrollBar = true;
toReturn.VScrollBar = true;
if (lineNumbers)
toReturn.Margins[0].Width = 40; //allows display of line numbers
else
foreach (var margin in toReturn.Margins)
margin.Width = 0;
toReturn.ClearCmdKey(Keys.Control | Keys.S); //prevent Ctrl+S displaying ascii code
toReturn.ClearCmdKey(Keys.Control | Keys.R); //prevent Ctrl+R displaying ascii code
toReturn.ClearCmdKey(Keys.Control | Keys.W); //prevent Ctrl+W displaying ascii code
显示后,您可以在此处查看有关自动代码突出显示的文档 https://github.com/jacobslusser/ScintillaNET/wiki/Custom-Syntax-Highlighting