C#RichTextBox选择指定的文本 [英] C# RichTextBox Select specified Text

查看:84
本文介绍了C#RichTextBox选择指定的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RichTextBox,带有-通过示例-此文本:

I have a RichTextBox with -by example- this text:

"This is my Text"

现在,我想搜索" RichTextBox中的文本(字符串),例如:

Now I want to "search" the RichTextBox for a Text (String), by example:

"Text"

现在应该在RichTextBox中选中/突出显示文本"..

Now "Text" should be selected/highlighted (for each one) in the RichTextBox..

有类似的东西

myRichTextBox.Select();

但是在这里我必须设置一个StartPosition等等,但是我想搜索String!

but here I have to set a StartPosition and so on, but I want to search for String!

我该怎么做?(搜索了stackoverflow,没有找到类似的东西.)

How could I do this? (Searched stackoverflow, didn't find something similiar..)

推荐答案

     int start = 0;
int indexOfSearchText = 0;
private void btnFind_Click(object sender, EventArgs e)
        {
            int startindex = 0;

            if(txtSearch.Text.Length > 0)
                startindex = FindMyText(txtSearch.Text.Trim(), start, rtb.Text.Length);

            // If string was found in the RichTextBox, highlight it
            if (startindex >= 0)
            {
                // Set the highlight color as red
                rtb.SelectionColor = Color.Red;
                // Find the end index. End Index = number of characters in textbox
                int endindex = txtSearch.Text.Length;
                // Highlight the search string
                rtb.Select(startindex, endindex);
                // mark the start position after the position of
                // last search string
                start = startindex + endindex;
            }
        }




public int FindMyText(string txtToSearch, int searchStart, int searchEnd)
        {
            // Unselect the previously searched string
            if (searchStart > 0 && searchEnd > 0 && indexOfSearchText >= 0)
            {
                rtb.Undo();
            }

            // Set the return value to -1 by default.
            int retVal = -1;

            // A valid starting index should be specified.
            // if indexOfSearchText = -1, the end of search
            if (searchStart >= 0 && indexOfSearchText >=0)
            {
                // A valid ending index
                if (searchEnd > searchStart || searchEnd == -1)
                {
                    // Find the position of search string in RichTextBox
                    indexOfSearchText = rtb.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None);
                    // Determine whether the text was found in richTextBox1.
                    if (indexOfSearchText != -1)
                    {
                        // Return the index to the specified search text.
                        retVal = indexOfSearchText;
                    }
                }
            }
            return retVal;
        }



private void textBox1_TextChanged(object sender, EventArgs e)
        {
            start = 0;
            indexOfSearchText = 0;
        }

如果您不了解此代码,请查看本文... http://www.dotnetcurry.com/ShowArticle.aspx?ID=146

CheckOut this article if you dont understand this code... http://www.dotnetcurry.com/ShowArticle.aspx?ID=146

这篇关于C#RichTextBox选择指定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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