C# 窗体编程 Windows编程 richTextBox的Find方法怎么用??

就是想做一个记事本的查找功能,输入一个字符串,然后查找,查到后选中显示??怎么做??... 就是想做一个记事本的查找功能,输入一个字符串,然后查找,查到后选中显示??怎么做?? 展开
 我来答
百度网友d038aa7
推荐于2018-04-24 · TA获得超过471个赞
知道小有建树答主
回答量:1129
采纳率:64%
帮助的人:244万
展开全部

给你一段代码,我以前写的,可以往前跟往后查找

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyTxt
{
    public partial class find : Form
    {
        public find()
        {
            InitializeComponent();
        }
        Form1 f1;
        int index = 0;
        private void button1_Click(object sender, EventArgs e)
        {

            if (radioButton1.Checked)
            {
                if (textBox1.Text.Length <= 0)
                    return;
                if (index == 0)//如果光标在最开始
                {
                    MessageBox.Show("未找到");
                    return;
                }
                //搜索的起点
                int startIndex = index - textBox1.Text.Length;
                if (startIndex < 0)
                    startIndex = 0;
                //搜索
                index = f1.richTextBox1.Text.IndexOf(textBox1.Text, startIndex);

                if (index == -1)
                {
                    MessageBox.Show("未找到");
                    index = f1.richTextBox1.Text.IndexOf(f1.richTextBox1.SelectedText);
                }
                else
                {
                    f1.richTextBox1.Select(index, textBox1.Text.Length);//选中文本
                    index -= 1;
                    if (index < 0)
                        index = 0;
                    f1.Activate();
                }
            }
            else 
            {
             
                if (textBox1.Text.Length <= 0)
                    return;
                index = f1.richTextBox1.Text.IndexOf(textBox1.Text, index);
                if (index == -1)
                {
                    MessageBox.Show("未找到");
                    index = f1.richTextBox1.Text.LastIndexOf(f1.richTextBox1.SelectedText) + 1;
                }
                else
                {
                    f1.richTextBox1.Select(index, textBox1.Text.Length);//选择文本框中的文本
                    index += textBox1.Text.Length;//往下找
                    f1.Activate();
                }
             
            
            }
        }

        private void find_Load(object sender, EventArgs e)
        {
            f1 = (Form1)this.Owner;
                index = f1.myindex;
             
        
        }
    }
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式