1

txtComKeyword1XMLファイルを検索して、これらのテキストボックス、、、および/またはtxtComKeyword2に挿入された単語に一致するコンテンツがあるかどうかを確認しています。以下の機能は機能していますが、リッチテキストボックスに表示される一致する4つのテキストボックスにユーザーが入力したキーワードを強調表示するにはどうすればよいですか?txtComKeyword3txtComKeyword4richComResults

たとえば、私のユーザーはこれらの4つのテキストボックスに入力します。txtComKeyword1、txtComKeyword2、txtComKeyword3およびtxtComKeyword4。次に、コードはXMLファイルを解析して、ノードにこれらの4つのキーワードが含まれているかどうかを確認します。含まれている場合は、ノードのデータがrichComResultsに出力されます。これらの4つのキーワードを強調表示します(例:txtComKeyword1 = hello、txtComKeyword2 = bye、txtComKeyword3 =朝、txtComKeyword4 =夜)。これらの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");
                }
            }
    }
}
4

3 に答える 3

3

これを試して:

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;
}

これにより、キーワードが検索され、強調表示されます。次に、見つかったキーワードの後に​​検索を続行します。ポインタは、テキスト内の検索位置を追跡するために使用されます。インデックスは、見つかったキーワードの位置を示します。

于 2012-08-10T03:09:29.433 に答える
0

このコードを試してください:

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";
    }

文字列strをメソッドで埋めた後、myメソッドを呼び出します。

string strRich =

            "Author : Habib\nDate : 2012-08-10 \nTitle : mytitle \nDescription : desc\n";


        Regex r = new Regex("\\n");
        String[] lines = r.Split(strRich);

        foreach (string l in lines)
        {
            ParseLine(l);
        }

楽しい。

于 2012-08-10T03:13:13.473 に答える
0

Janの答えには素晴らしい内容が含まれていますが、私はしばらくの間(本当)穏やかに身震いし、側面を壊しました!これが私の微調整された(大文字と小文字を区別しない)バージョンです...

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);
}
于 2018-05-15T08:01:28.360 に答える