如何从richtextbox中仅删除Strikeout的文本? [英] How can I remove from richtextbox only the text that's Strikeout ?

查看:90
本文介绍了如何从richtextbox中仅删除Strikeout的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我有一个richtextbox的问题,我试图只删除Strikeout的单词,但如果我有一个单词出现与普通字体,那么它的单词也被删除。如何删除Strikeout中的单词?



这是我的代码:



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:

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

推荐答案

代码的主要问题是分裂为单词。您必须仅在搜索标记中搜索删除属性和清除文本。但这有时候开始或结束的另一个标签可以在strike标签中。你必须检查一下。



这段代码可以帮到你:)



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 :)

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


我还没有测试过您的代码,但是一个明显的错误是您不会更改richtextbox文本,而是更改其复制的值。因此,你永远不会看到删除删除文本。请尝试以下代码:



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:

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


这篇关于如何从richtextbox中仅删除Strikeout的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆