Trishka1111
Trishka1111

Reputation: 77

How to delete a word in RichTextBox on button click?

I am making a tiny app in C# that i have a text box, 3 buttons and every button is a specific bad word. If I click on it it will find this word in a text and deletes it.

button 1 is named btn1 and button 2 btn2 and button3 btn3. I was thinking of using RichTextBoxFinds but I am stucked right at the beginning.

    private void btn1(object sender, EventArgs e)
    {
        Button btn1 = sender as Button;
        string word1 = btn1.Text;
      if (RichTextBox.Find(word1) = true)
        {
            RichTextBox.Delete(word1);
        }
    }

Upvotes: 0

Views: 980

Answers (2)

Harald Coppoolse
Harald Coppoolse

Reputation: 30454

I'm not sure if it is wise to just remove bad words from a text, without giving the user the opportunity to decide about alternative descriptions. Just removing the words might make it unreadable, or even give it the opposite meaning. But hey, it is your requirement.

Let's first create a reusable method to remove words from any RichTextBox, not just your RichTextBox. I'll do this as an extension method so you can use it for every RicthTextBox in your application. See extension methods demystified

public static void RemoveWord(this RichTextBox richTextBox, string wordToRemove)
{
    // We only want to find whole words. Don't want any flickering highlights
    RichTextBoxFinds finds = RichTextboxFinds.WholeWord | RichTextBoxFinds.NoHighLight;
    int wordIndex = richTextBox.Find(wordToRemove, finds);
    while (wordIndex >= 0)
    {
        // occurence found.
        // in RichTextBox a word is removed by selecting it, and replace it
        richTextBox.Select(wordIndex, wordToRemove.Length);
        richTextBox.SelectedText = String.Empty;

        // find the next bad word:
        wordIndex = richTextBox.Find(wordToRemove, finds);
    } 
}

Usage:

RichTextBox myRichTextBox = ...
string wordToRemove = ...

myRichTextbox.RemoveWord(wordToRemove);

Kinda neat, isn't it? It looks like as if RemoveWord is a standard method of RichTextbox.

Back to your question

So you have several buttons, and every button has the functionality that it should remove all occurences of a certain word from a RichTextBox. This looks like a special kind of Button. Consider to create a special class for it.

class RemoveBadWordButton : Button
{
    public string BadWord {get; set;}
    public RichTextBox RichTextBox {get; set;}

    protected override OnButtonClicked()
    {
         this.RichTextBox.RemoveWord(this.BadWord);
    }
}

If the changes are small, people tend not to create special classes for it, but let the Form that owns the button do the special functionality. If you choose for this, consider using Button.Tag to assign the word that must be removed.

button1.Tag = "badword1";
button2.Tag = "badword2";
this.button1.Click += this.OnBadWordButtonClicked);
this.button2.Click += this.OnBadWordButtonClicked);

private void OnBadWordButtonClicked(object sender, EventArgs e)
{
    Button badWordButton = (Button)sender;
    string badWord = badWordButton.Tag.ToString();
    this.RichTextBox1.RemoveWord(badWord);
}

Advantage: only one method for all you BadWord buttons. If you decide to add another bad word removal button, code changes are minimal.

Upvotes: 1

vers
vers

Reputation: 664

Try this (assuming your RichTextBox is named as richTextBox1):

private void btn1(object sender, EventArgs e)
{
    Button btn = sender as Button;
    string wordToRemove = btn.Text;
    RemoveWordFromRichTextBox(richTextBox1, wordToRemove);
}

private void RemoveWordFromRichTextBox(RichTextBox richTextBox, string word)
{
    int index;
    do
    {
        index = richTextBox.Find(word);
        if (index >= 0)
        {
            richTextBox.Select(index, word.Length);
            richTextBox.SelectedText = String.Empty;
        }
    }
    while (index >= 0);
}

This should work properly for the formatted text as well (e.g. for colored text, bold, italic, etc.)

Upvotes: 1

Related Questions