C# 在RichTextBox中突出显示用户定义的关键字
我正在搜索XML文件,以查看是否有与这些文本框中插入的单词匹配的内容C# 在RichTextBox中突出显示用户定义的关键字,c#,winforms,C#,Winforms,我正在搜索XML文件,以查看是否有与这些文本框中插入的单词匹配的内容txtComKeyword1,txtComKeyword2,txtComKeyword3和/或txtComKeyword4。下面的函数正在运行,但我可以知道如何突出显示用户在我的richComResultsrichtextbox中出现的四个匹配文本框中输入的关键字吗 例如,我的用户将填写这四个文本框,即txtComKeyword1、txtComKeyword2、txtComKeyword3和txtComKeyword4。然后,我
txtComKeyword1
,txtComKeyword2
,txtComKeyword3
和/或txtComKeyword4
。下面的函数正在运行,但我可以知道如何突出显示用户在我的richComResults
richtextbox中出现的四个匹配文本框中输入的关键字吗
例如,我的用户将填写这四个文本框,即txtComKeyword1、txtComKeyword2、txtComKeyword3和txtComKeyword4。然后,我的代码将解析XML文件以查看节点是否包含这四个关键字,如果是,节点的数据将输出到我的richComResults上,我想突出显示这四个关键字(例如txtComKeyword1=hello,txtComKeyword2=bye,txtComKeyword3=morning,txtComKeyword4=night)。这4个单词,如果在richComResults中找到并出现,将以颜色突出显示
搜索了一段时间后,我没有任何线索,我的案例与其他问题大不相同。我是一名编程新手,非常感谢您的帮助。谢谢大家!
我的代码:
private void searchComByKeywords()
{
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(sourceDir);
foreach (string fileName in fileEntries)
{
XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.
string docPath = fileName;
xmlDoc.Load(docPath); //* load the XML document from the specified file.
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("item");
foreach (XmlNode node in nodeList)
{
XmlElement itemElement = (XmlElement) node;
string itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;
if (txtComKeyword1.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword1.Text.ToLower()) ||
txtComKeyword2.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword2.Text.ToString()) ||
txtComKeyword3.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword3.Text.ToString()) ||
txtComKeyword4.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword4.Text.ToString()))
{
string itemTitle = itemElement.GetElementsByTagName("title")[0].InnerText;
string itemDate = itemElement.GetElementsByTagName("pubDate")[0].InnerText;
string itemAuthor = itemElement.GetElementsByTagName("author")[0].InnerText;
richComResults.AppendText("Author: " + itemAuthor + "\nDate: " + itemDate + "\nTitle: " + itemTitle + "\nDescription: " + itemDescription + "\n\n--------\n\n");
}
}
}
}
试试这个:
int pointer = 0;
int index = 0;
string keyword = "txtComKeyword1";
while (true)
{
index = richComResults.Text.IndexOf(keyword, pointer);
//if keyword not found
if (index == -1)
{
break;
}
richComResults.Select(index, keyword.Length);
richComResults.SelectionFont = new System.Drawing.Font(richComResults.Font, FontStyle.Bold);
pointer = index + keyword.Length;
}
这将搜索关键字并将其高亮显示。然后在找到的关键字后继续搜索。指针用于跟踪文本中的搜索位置。索引标记找到的关键字的位置。请尝试以下代码:
void ParseLine(string line)
{
Regex r = new Regex("([ \\t{}():;])");
String[] tokens = r.Split(line);
foreach (string token in tokens)
{
// Set the tokens default color and font.
richTextBox1.SelectionColor = Color.Black;
richTextBox1.SelectionFont = new Font("Courier New", 10, FontStyle.Regular);
// Check whether the token is a keyword.
String[] keywords = { "Author", "Date", "Title", "Description", };
for (int i = 0; i < keywords.Length; i++)
{
if (keywords[i] == token)
{
// Apply alternative color and font to highlight keyword.
richTextBox1.SelectionColor = Color.Blue;
richTextBox1.SelectionFont = new Font("Courier New", 10, FontStyle.Bold);
break;
}
}
richTextBox1.SelectedText = token;
}
richTextBox1.SelectedText = "\n";
}
享受。简的回答包含了很好的内容,但我在中途(真的)和休息时有点发抖!这是我经过调整的(不区分大小写)版本
int nextHigh = RTF.Text.IndexOf(txSearch, 0, StringComparison.OrdinalIgnoreCase);
while (nextHigh >= 0)
{
RTF.Select(nextHigh, txSearch.Length);
RTF.SelectionColor = Color.Red; // Or whatever
RTF.SelectionFont = new Font("Arial", 12, FontStyle.Bold); // you like
nextHigh = RTF.Text.IndexOf(txSearch, nextHigh + txSearch.Length, StringComparison.OrdinalIgnoreCase);
}
但我想要的关键词并不完全是“txtComKeyword1”txtComKeyword1是允许用户输入单词的文本框。然后只需更改一行:
string keyword=txtComKeyword1。Text
如果它说“当前上下文中不存在名称字体样式”?:|我的使用System.Windows.Forms代码>存在。您需要使用System.Drawing代码>以便使用FontStyle
Yes。我已经从我的一个工作程序中复制了代码,所以我提供的工作正常。你只需要适应你的情况。也许这与区分大小写有关?请尝试string keyword=txtComKeyword1.Text.ToLower()
。如果这不起作用,你必须逐行调试你的代码,并验证你的变量值是否符合你的期望。这并不真正反映我的需要:|我的用户将填写这四个文本框,即txtComKeyword1
,txtComKeyword2
,txtComKeyword3
和txtComKeyword4
。然后,我的代码将解析XML文件以查看节点是否包含这四个关键字,如果是,节点数据将输出到我的richComResults
,我想突出显示这四个关键字(例如txtComKeyword1=hello,txtComKeyword2=bye,txtComKeyword3=morning,txtComKeyword4=night)。这4个单词(如果在richComResults中找到并出现)将以颜色突出显示。
int nextHigh = RTF.Text.IndexOf(txSearch, 0, StringComparison.OrdinalIgnoreCase);
while (nextHigh >= 0)
{
RTF.Select(nextHigh, txSearch.Length);
RTF.SelectionColor = Color.Red; // Or whatever
RTF.SelectionFont = new Font("Arial", 12, FontStyle.Bold); // you like
nextHigh = RTF.Text.IndexOf(txSearch, nextHigh + txSearch.Length, StringComparison.OrdinalIgnoreCase);
}