Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I have a little problem with a richtextbox, i tried to remove only the words that are Strikeout, but if i have a word that appears with Regular font then the word it's removed too. How can I remove only the words in Strikeout?

Here is my code:

C#
text = richTextBox2.Text;
            
            string[] destLine = richTextBox2.Text.Split(WordSeparaterss);
            foreach (string str in destLine)
            {
                int startIndex = 0;
                while (startIndex != -1)
                {
                    startIndex = richTextBox2.Find(str, startIndex + 1,
                                    richTextBox2.Text.Length,
                                    RichTextBoxFinds.WholeWord);
                    if (startIndex != -1)
                    {
                        richTextBox2.Select(startIndex, str.Length);

                        if (richTextBox2.SelectionFont.Strikeout)
                        {
                            text = text.Replace(str, string.Empty);
                        }
                    }
                }
            }
Posted

Main problem of your code is splitting to words. You must search for only strike attribute and clear text in strike tags. But this sometimes start or end of another tag can be in of the strike tag. You must check that.

This code can help you :)

C#
richTextBox1.Rtf = Regex.Replace(richTextBox1.Rtf, @"(?<=\\strike).*?(?=\\strike0)", delegate(Match match)
            {
                string v = match.ToString();
                var matches = Regex.Matches(v, @"([\\].*?(?=[\\\ ]))");
                string v1 = string.Empty;
                foreach (Match m in matches)
                {
                    v1 += m.Value;
                }
                return v1;
            });
 
Share this answer
 
Comments
Irina08 25-Jun-14 8:00am    
Thank you. It's working. :)
I haven't tested your code, but an obvious mistake is that you don't change richtextbox text but its copied value. therefore, you never see strike-out text removed. try the following code:

C#
private void buttonRemove_Click(object sender, EventArgs e)
{
    int pos = 0, lastpos = 0;
    char[] sep = new char[] { ' ' };

    // while separator found
    while ((lastpos = this.richTextBox1.Find(sep, pos)) > pos)
    {
        try
        {
            // skip consecutive separators
            if (lastpos - pos == 1)
            {
                continue;
            }

            // try to remove strikeout text
            try
            {
                this.richTextBox1.Select(pos, lastpos - pos);
                if (this.richTextBox1.SelectionFont.Strikeout)
                    this.richTextBox1.SelectedText = string.Empty;
            }
            catch (ArgumentOutOfRangeException)
            {
                break;
            }
        }
        finally
        {
            // advance pos for every search
            pos = lastpos + 1;
        }
    }

    // check for last word
    if (pos + 1 < this.richTextBox1.Text.Length)
    {
        this.richTextBox1.Select(pos, this.richTextBox1.Text.Length - pos);
        if (this.richTextBox1.SelectionFont.Strikeout)
            this.richTextBox1.SelectedText = string.Empty;
    }
}
 
Share this answer
 
Comments
Irina08 25-Jun-14 6:35am    
I run you code and for some words is working and for some it doesn't.
It appaers this exception:" Object reference not set to an instance of an object." on : if (this.richTextBox1.SelectionFont.Strikeout)
in the if (pos + 1 <this.richTextBox1.Text.Length)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900