【问题标题】:how to find and replace text in richtextbox如何在richtextbox中查找和替换文本
【发布时间】:2018-09-04 18:37:06
【问题描述】:

我正在开发一个 C# windows 窗体,它执行以下操作:

我有一个RichTextbox,它显示一个文本文件的内容。

我设法构建了一个带有搜索按钮的搜索框,供用户在此文件中搜索特定文本。但是,我还想创建一个查找文本框和按钮,以允许用户用他/她在文本框中输入的新文本替换找到的文本并单击替换按钮。我该怎么做才能替换文本? ... 谢谢。

这里是搜索文本的代码:

 private void buttonSearch_Click(object sender, EventArgs e)
 {
     int index = 0;
     var temp = richTextArea.Text;
     richTextArea.Text = "";
     richTextArea.Text = temp;

     while (index < richTextArea.Text.LastIndexOf(textBoxSearch.Text))
     {
         richTextArea.Find(textBoxSearch.Text, index, richTextArea.TextLength, RichTextBoxFinds.None);
         richTextArea.SelectionBackColor = Color.Yellow;

         index = richTextArea.Text.IndexOf(textBoxSearch.Text, index) + 1;
     }
 }

wnform

【问题讨论】:

    标签: c#


    【解决方案1】:

    我有一个有效的答案给你,在这里:

        public static void QuickReplace(RichTextBox rtb, String word, String word2)
        {
            rtb.Text = rtb.Text.Replace(word, word2);
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
                QuickReplace(richTextBox1, textBox1.Text, textBox2.Text);
        }
    

    用你的RichTextBoxControl替换richTextBox1,用你的TextBoxControls替换textBox1textBox2

    这将替换RichTextBox中找到的所有所需文本

    我专门为这种操作编写了这段代码。

    如果您愿意,我可以提供用于替换其他表单中的文本的代码,例如 记事本

    希望对你有帮助:)

    【讨论】:

      【解决方案2】:

      如果要替换文本中所有出现的搜索词:

      richTextArea.Text = richTextArea.Text.Replace(textBoxSearch.Text, replaceTextBox.Text);
      

      【讨论】:

      • 非常感谢您的帮助
      • 当然,但是,我试图用新名称替换图像 (fig1.png)。我有不止一个图像,除了如果你搜索“fig”并用你的新文本替换它会替换它为图像和任何其他包含“fig”的单词..有没有办法解决这个问题。 .. 再次感谢您的帮助。
      • 你有这样的文字:“fig1.img some text figo fig1.txt fig3.img。”您只想更改 fig1.img 和 fig3.img?
      • 是的,我只想用新名称替换图像名称“fig”,但是数字保持不变。
      【解决方案3】:

      我添加了两个按钮,一个用于将文件内容加载到富文本框中,另一个用于查找和替换文本并将替换的内容再次写入文件。

      private void Load_File_Contents_Click(object sender, EventArgs e)
          {
              try
              {
                  //Below code will read the file and set the rich textbox with the contents of file
                  string filePath = @"C:\New folder\file1.txt";
                  richTextBox1.Text = File.ReadAllText(filePath);
              }
              catch (Exception ex)
              {
                  lblError.Text = ex.Message;
              }
          }
      
          private void ReplaceAndWriteToFile_Click(object sender, EventArgs e)
          {
              try
              {
                  string filePath = @"C:\New folder\file1.txt";
      
                  //Find the "find" text from the richtextbox and replace it with the "replace" text
                  string find = txtFind.Text.Trim(); //txtFind is textbox and will have the text that we want to find and replace
                  string replace = txtReplace.Text.Trim(); //txtReplace is text and it will replace the find text with Replace text
                  string newText = richTextBox1.Text.Replace(find, replace);
                  richTextBox1.Text = newText;
      
                  //Write the new contents of rich text box to file
                  File.WriteAllText(filePath, richTextBox1.Text.Trim());
              }
              catch (Exception ex)
              {
                  lblError.Text = ex.Message;
              }
          }
      

      【讨论】:

      • 非常感谢您的帮助
      • 谢谢。我使用 filedialog 方法加载文件,您的解决方案也运行良好,谢谢,但是,我试图用新名称替换图像(fig1.png)。我有不止一个图像,除了如果你搜索“fig”并用你的新文本替换它会替换它为图像和任何其他包含“fig”的单词..有没有办法解决这个问题。 .. 再次感谢您的帮助。
      猜你喜欢
      • 2018-12-31
      • 2013-07-18
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      • 1970-01-01
      相关资源
      最近更新 更多