用C#做了一个文本编辑器,
现在在菜单栏里加了一个“查找替换”的选项,现在想要实现的代码
谢谢

解决方案 »

  1.   

    public int start;
    button1的click事件
    {string str1;             //存放要查找的文本
    str1=textBox2.Text;      //获取要查找的文本
    start=richTextBox1.Find(str1,start,RichTextBoxFinds.MatchCase);
    if(start==-1)
    {
       MessageBox.Show("已经查找到文件末尾","");
       start=0;
    }
    else 
    {
       start=start+str1.Length;
       richTextBox1.Focus();  //设置焦点
    }
    }
    textBox2的TextChanged事件
    {start=0;//只要查找的文本改变,则把start位置为0}
    button2的click事件
    {
       string str1,str2;
       string str1=textBox2.Text;
       string str2=textBox3.Text;
       start=richTextBox1.Find(str1,start,RichTextBoxFinds.MatchCase);   //查找下一个
       while (start!=1)
         {
            richTextBox1.SelcetedText=str2;                  //查找下一个
            start+=str2.Length;                            //下一次查找的其实位置
            start=richTextBox1.Find(str1,start,RichTextBoxFinds.MatchCase);
          }
       MessageBox.Show("已经查找到文件末尾","");
       start=0;
       richTextBox1.Focus();                //设置焦点
    }//以上是手写代码,如有错,自己更改
      

  2.   

    想问一下 windows这样的类没有现成的吗?一定要自己写吗?
      

  3.   

    记得有查找的api,但查找替换的没有用过,不知道有没有.
    但这个东西不复杂,自己写一个吧.
      

  4.   

    啊,真不巧。.NET Framework里richtextbox果然没有replace方法。
    要不你试一下string的replace方法?
      

  5.   

    private void btReplace_Click(object sender, EventArgs e)
            {
                string s = richTextBox1.Text;
                string sFind = textBox1.Text;
                string sReplace = textBox2.Text;
                s = s.Replace(sFind, sReplace);
                richTextBox1.Text = s;
            }