如何在富文本框中找到特定的单词,按行分开 [英] how to find a specif word in a richtext box libe by line click

查看:75
本文介绍了如何在富文本框中找到特定的单词,按行分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个富文本框,一行一行地写着10个句子,我还有一个文本框,我在其中写一个单词可以在富文本框中找到.
当我单击按钮时,我想在每次单击中逐行突出显示所有特定单词.
如果在任何一行中该特定单词有两次,那么我要单击一下以突出显示两个单词.

我是编码新手.
请帮帮我.


我正在使用此代码,只需单击一下即可突出显示所有特定单词.
但是现在我想在每次点击中逐行搜索.

Hi i have a rich text box there are 10 sentences written line by line and i have also one text box where i write a word to find in rich text box.
when i click on button then i want to highlight all specific word line by line in every click.
and if in any line there are two times that specific word then i want to highlight both word in a single click.

i am new in coding.
Please help me.


I am using this code it highlights all the specific word at a single click.
but now i want to search line by line in every click.

int textEnd = txtshowfile.TextLength;
                    int index = 0;
                    int lastIndex = txtshowfile.Text.LastIndexOf(txtsearchgerman.Text);
                    while (index < lastIndex)
                    {
                        txtshowfile.Find(txtsearchgerman.Text, index, textEnd, RichTextBoxFinds.None);
                        txtshowfile.SelectionBackColor = Color.Yellow;
                        index = txtshowfile.Text.IndexOf(txtsearchgerman.Text, index) + 1;
                    }





在此先感谢您的朋友.





Thanks in Advance friends.

推荐答案

这里是一些代码的草图,希望可以帮助您入门.

在WinForm上放置一个RichTextBox,一个Button和一个TextBox:将它们命名为:"richTextBox1," button1和"textBox1:
Here''s a sketch of some code which, I hope, will get you started.

Put a RichTextBox, a Button, and a TextBox on a WinForm: name them: ''richTextBox1, ''button1, and ''textBox1:
private string searchTerm;

private string theLine;

private int maxLines;
private int targetLine = 0;
private int matchAt;
private int searchTermLength;
private int lineLength;
private int lineOffset = 0;

private void button1_Click(object sender, EventArgs e)
{
    maxLines = richTextBox1.Lines.Length;

    searchTerm = textBox1.Text;

    searchTermLength = searchTerm.Length;

    theLine = richTextBox1.Lines[targetLine];

    lineLength = theLine.Length;

    if(theLine.Contains(searchTerm))
    {
         matchAt = theLine.IndexOf(searchTerm);

         richTextBox1.Select(lineOffset + matchAt, searchTermLength);

         richTextBox1.SelectionBackColor = Color.Yellow;
         
         // at this point you really have all the information you need to 
         // to keep scanning the current line for more search term matches !
         // personally, I'd use a while loop starting here ...
    }
 
    // note we don't do anything here if no match is found in the current line

    targetLine ++;

    // consider why we must add #1 here
    lineOffset += lineLength + 1;

    if (targetLine == maxLines)
    {
        targetLine = 0;
        lineOffset = 0;
    }
}

此代码示例将在RichTextBox中逐行找到搜索词的第一个示例,并将其突出显示为黄色.

它不做的是:

1.提供任何指示,表明它刚刚检查了没有搜索词的行:如果您希望它跳过"没有搜索词匹配的行,并自动前进到下一行:你去做.

2.找到搜索词的多个匹配项:这剩下的要弄清楚,但是一旦您隔离"了当前行,并在代码中计算出了可以看到的信息:我相信不会困难.

3.处理RichTextBox内容更改的情况,并且您需要重置所有用于搜索的参数.并取消突出显示任何当前突出显示的匹配项吗?

4.处理搜索项已更改的情况,您需要重置所有用于搜索的参数.并取消突出显示任何当前突出显示的匹配项吗?

5.任何类型的错误检查和确认,都可以使用真正可靠的解决方案.

一个真正稳健"的解决方案可能需要跟踪所有当前突出显示的匹配项,因此容易将它们取消突出显示吗?我可能会考虑建立一个List< int>每个高亮显示匹配项,其中整数值是RichTextBox.Text中匹配开始的偏移量的绝对值:假设搜索项长度是一个常数,那么您需要执行的是foreach遍历通用List< int> ;,并选择突出显示的单词,然后重置颜色.

或者,更简单地说,只需选择RichTextBox中的所有Text并将其SelectionBackColor更改为某个默认值?

This code example will find, line by line, the first example of the search term in the RichTextBox, and highlight it yellow.

What it does not do is:

1. give any indication that it just checked a line which did not have the search term: if you want it to "skip over" lines that have no search term match, and automatically advance to the next line: that''s left for you to do.

2. find multiple matches of the search term: that''s left to you to figure out, but once you have "isolated" the current line, and have the information you can see calculated in the code: I believe that will not be difficult.

3. deal with the case where the RichTextBox content is changed, and you need to reset all the parameters for searching. And un-highlight any currently highlighted matches ?

4. deal with the case where the search term has changed, and you need to reset all the parameters for searching. And un-highlight any currently highlighted matches ?

5. any of the type of error-checking and validation a truly robust solution would do.

A really "robust" solution may need to keep track of all currently highlighted match terms, so it''s easy to un-highlight them ? I might consider building a List<int> of each highlight match, where the integer value was the absolute value of the offset of the start of the match in the RichTextBox.Text: given the assumption that the search-term length is a constant, then all you need to do is execute a foreach loop through the generic List<int>, and select the high-lighted word, and reset the color.

Or, much more simply, just select all the Text in the RichTextBox and change its SelectionBackColor to some default value ?


如果要在选定的行上搜索文本,则必须保持总数行和鼠标在哪一行,如果您想搜索全文,那么这些链接将为您提供帮助
http://support.microsoft.com/kb/176643 [ http://www.dotnetcurry.com/ShowArticle.aspx?ID=146 [ ^ ]
If you want to search text on selected line then you have to maintain total number of lines and in which row your mouse is, and if you want to search whole text then these links will help you
http://support.microsoft.com/kb/176643[^]
http://www.dotnetcurry.com/ShowArticle.aspx?ID=146[^]


这篇关于如何在富文本框中找到特定的单词,按行分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆