如何维护富文本格式(粗体/斜体/等)改变任何一个元素的时候? [英] How do I maintain RichText formatting (bold/italic/etc) when changing any one element?

查看:799
本文介绍了如何维护富文本格式(粗体/斜体/等)改变任何一个元素的时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有可能包含有黑体,斜体,甚至是不同的字体和大小元素的字符串格式文本框。如果我选择整个字符串,包括所有的差异,哪能大胆的字符串没有整个字符串只是一个大胆的属性转换为普通字体?

例如:我想打开这的某种文本此的某种 文本

请注意,是一些保持斜体和文本仍然是一个不同的字体。

我现在有相当简单:

 私人无效tsBold_Click(对象发件人,EventArgs的)
{
    如果(rtb.SelectionFont == NULL)回报;

    字体F;

    如果(tsBold.Checked)
        F =新的字体(rtb.SelectionFont,FontStyle.Bold);
    其他
        F =新的字体(rtb.SelectionFont,FontStyle.Regular);

    rtb.SelectionFont = F;

    rtb.Focus();
}
 

当然,这是要完全一样的字体应用到整个选择。有没有什么办法,只是追加大胆到现有的字体(S)?

答案 而下面的官方的答案是冰山的一角,这是我需要在正确的方向推。谢谢你的提示。

下面是我的官方修正:

我说这对我的RichTextBox对象:

  ///<总结>
    ///更改RichTextBox的样式为当前选择
    ///< /总结>
    公共无效ChangeFontStyle(fontstyle的风格,布尔补充)
    {
        //此方法处理时,多种字体/样式选择出现的情况下,
        //参数: - 
        //风格 - 例如FontStyle.Bold
        //添加 - 如果为true再加入别的删除

        //抛出错误,如果风格是不是:粗体,斜体,删除线或下划线
        如果(风格!= FontStyle.Bold
            &功放;&安培;风格!= FontStyle.Italic
            &功放;&安培;风格!= FontStyle.Strikeout
            &功放;&安培;风格!= FontStyle.Underline)
            抛出新System.InvalidProgramException(无效样式参数ChangeFontStyle);

        INT rtb1start = this.SelectionStart;
        INT LEN = this.SelectionLength;
        INT rtbTempStart = 0;

        //如果len< = 1,并有选择的字体,然后只处理与回报
        如果(LEN< = 1&安培;&安培;!this.SelectionFont = NULL)
        {
            //添加或删除样式
            如果(添加)
                this.SelectionFont =新的字体(this.SelectionFont,this.SelectionFont.Style |样式);
            其他
                this.SelectionFont =新的字体(this.SelectionFont,this.SelectionFont.Style和放大器;〜样式);

            返回;
        }

        使用(EnhancedRichTextBox rtbTemp =新EnhancedRichTextBox())
        {
            //通过选定的文本一个字符一步一个脚印
            rtbTemp.Rtf = this.SelectedRtf;
            的for(int i = 0; I< LEN ++ I)
            {
                rtbTemp.Select(rtbTempStart + I,1);

                //添加或删除样式
                如果(添加)
                    rtbTemp.SelectionFont =新的字体(rtbTemp.SelectionFont,rtbTemp.SelectionFont.Style |样式);
                其他
                    rtbTemp.SelectionFont =新的字体(rtbTemp.SelectionFont,rtbTemp.SelectionFont.Style和放大器;〜样式);
            }

            //更换和放大器;重新选择
            rtbTemp.Select(rtbTempStart,LEN);
            this.SelectedRtf = rtbTemp.SelectedRtf;
            this.Select(rtb1start,LEN);
        }
        返回;
    }
 

我再变点击的方法来使用以下方式:

 私人无效tsBold_Click(对象发件人,EventArgs的)
    {
        enhancedRichTextBox1.ChangeFontStyle(FontStyle.Bold,tsBold.Checked);

        enhancedRichTextBox1.Focus();
    }
 

解决方案

RTB做的没有的支持这一点。你甚至不能发现的字符范围具有相同的字体样式的选择范围内。首先检查了SelectionFont财产,这将是空如果选择包含风格的混合体。如果是这样的话,你就必须通过设置SelectionStart和SelectionLength属性来遍历选择一个字符的时间,阅读SelectionFont,直到它的变化。应用更改字体,你发现的范围。

检查<一href="http://stackoverflow.com/questions/3282384/richtextbox-syntax-highlighting-in-real-time-disabling-the-repaint/3282911#3282911">this回答一种方法来保持这种合理的快速,无闪烁。

请注意,实施与RTB的编辑器是一个最喜欢的话题,在codeproject.com。借款code,如果不是整个项目,是减轻疼痛的好方法。

I have a rich text box that may contain a string that has elements of bold, italics, or even different fonts and sizes. If I select the entire string, including all of the differences, how can I "Bold" that string without converting the entire string to the generic font with just a "bold" attribute?

For example: I want to turn "This is some text" into "This is some text"

Note that "is some" remained italicized and "text" remained a different font.

What I currently have is quite simplistic:

private void tsBold_Click(object sender, EventArgs e)
{
    if (rtb.SelectionFont == null) return;

    Font f;

    if (tsBold.Checked)
        f = new Font(rtb.SelectionFont, FontStyle.Bold);
    else
        f = new Font(rtb.SelectionFont, FontStyle.Regular);

    rtb.SelectionFont = f;

    rtb.Focus();
}

Of course, this is going to apply the exact same font to the entire selection. Is there any way to just append "bold" to the existing font(s)?

ANSWER While the "official" answer below is just the tip of the iceberg, it was the push I needed in the right direction. Thank you for the tip.

Here's my official fix:

I added this to my RichTextBox object:

    /// <summary>
    ///     Change the richtextbox style for the current selection
    /// </summary>
    public void ChangeFontStyle(FontStyle style, bool add)
    {
        //This method should handle cases that occur when multiple fonts/styles are selected
        // Parameters:-
        //  style - eg FontStyle.Bold
        //  add - IF true then add else remove

        // throw error if style isn't: bold, italic, strikeout or underline
        if (style != FontStyle.Bold
            && style != FontStyle.Italic
            && style != FontStyle.Strikeout
            && style != FontStyle.Underline)
            throw new System.InvalidProgramException("Invalid style parameter to ChangeFontStyle");

        int rtb1start = this.SelectionStart;
        int len = this.SelectionLength;
        int rtbTempStart = 0;

        //if len <= 1 and there is a selection font then just handle and return
        if (len <= 1 && this.SelectionFont != null)
        {
            //add or remove style 
            if (add)
                this.SelectionFont = new Font(this.SelectionFont, this.SelectionFont.Style | style);
            else
                this.SelectionFont = new Font(this.SelectionFont, this.SelectionFont.Style & ~style);

            return;
        }

        using (EnhancedRichTextBox rtbTemp = new EnhancedRichTextBox())
        {
            // Step through the selected text one char at a time    
            rtbTemp.Rtf = this.SelectedRtf;
            for (int i = 0; i < len; ++i)
            {
                rtbTemp.Select(rtbTempStart + i, 1);

                //add or remove style 
                if (add)
                    rtbTemp.SelectionFont = new Font(rtbTemp.SelectionFont, rtbTemp.SelectionFont.Style | style);
                else
                    rtbTemp.SelectionFont = new Font(rtbTemp.SelectionFont, rtbTemp.SelectionFont.Style & ~style);
            }

            // Replace & reselect
            rtbTemp.Select(rtbTempStart, len);
            this.SelectedRtf = rtbTemp.SelectedRtf;
            this.Select(rtb1start, len);
        }
        return;
    }

I then changed the click methods to use the following pattern:

    private void tsBold_Click(object sender, EventArgs e)
    {
        enhancedRichTextBox1.ChangeFontStyle(FontStyle.Bold, tsBold.Checked);

        enhancedRichTextBox1.Focus();
    }

解决方案

RTB does not support this well. You cannot even discover the range of characters within the selection that has the same font style. Start by checking the SelectionFont property first, it will be null if the selection contains a mix of styles. If that's the case, you'll have to iterate the selection one character at a time by setting the SelectionStart and SelectionLength properties, read the SelectionFont until it changes. Apply the changed font to the range you discovered.

Check this answer for a way to keep this reasonably quick and flicker-free.

Note that implementing an editor with RTB is a favorite topic at codeproject.com. Borrowing code, if not the entire project, is a good way to lessen the pain.

这篇关于如何维护富文本格式(粗体/斜体/等)改变任何一个元素的时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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