Hanz Husseiner
Hanz Husseiner

Reputation: 33

how to find and replace text in richtextbox

I am working on a C# windows form, that does the following:

I have a RichTextbox that displays the contents of a text file.

I managed to build a search box with search button for the user to search for a specific text in this file. However, I want also to create a Find textbox and button to allow the user to replace that found text with new text he/she entered in the textbox and clicked replace button. How can I do that replace text? ... Thank you.

Here is the code for the search text:

 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

Upvotes: 1

Views: 11690

Answers (3)

Momoro
Momoro

Reputation: 617

I have an efficient answer for you, here:

    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);
    }

Replace richTextBox1 with your RichTextBox Control, and replace textBox1 & textBox2 with your TextBox Controls

This replaces ALL desired text found in a RichTextBox

I made this code specifically for this kind of Operation.

If you would like, I can provide code for replacing text from another form, i.e. just like Notepad.

I hope this helps you :)

Upvotes: 1

nkj
nkj

Reputation: 1

I have added two buttons, one to load the contents of file onto the rich text box and another one to Find and Replace the text and write the replaced contents to file again.

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;
        }
    }

Upvotes: 0

Django
Django

Reputation: 222

If you want to replace all occurences of search word in text:

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

Upvotes: 0

Related Questions