【问题标题】:How to change FontStyle back in RichTextBox?如何在 RichTextBox 中改回 FontStyle?
【发布时间】:2020-07-11 17:58:33
【问题描述】:

我正在使用单击按钮事件使选择文本变为粗体,它可以工作。但是当我选择它并再次单击相同的按钮时,我想让它变得正常,但它不起作用。

private void btnBold_Click(object sender, EventArgs e)
        {
            if (rtbMakale.SelectionLength > 0)
            {
                if (isItBold())
                {
                    rtbMakale.SelectionFont = new Font(rtbMakale.SelectionFont,
                        FontStyle.Regular | rtbMakale.SelectionFont.Style);
                    rtbMakale.SelectionLength = 0;
                }
                else
                {
                    rtbMakale.SelectionFont = new Font(rtbMakale.SelectionFont,
                        FontStyle.Bold | rtbMakale.SelectionFont.Style);
                    rtbMakale.SelectionLength = 0;
                }

            }

            bool isItBold()
            {
                if (rtbMakale.SelectionFont.Bold)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

【问题讨论】:

    标签: c# winforms text fonts richtextbox


    【解决方案1】:

    我已经编辑了代码,它功能齐全。使用它,您还可以轻松执行许多其他格式化操作。下面,我还添加了我从中得出此内容的链接。

    private void button1_Click(object sender, EventArgs e)
        {
    
            if (_richTextBox1.SelectionLength > 0)
            {
                if (_richTextBox1.SelectionFont.Bold == false)
                {
                    ChangeOrSetFont(_richTextBox1.SelectionFont.ToString(), _richTextBox1.SelectionFont.Size, FontStyle.Bold, true);
                }
                else
                {
                    ChangeOrSetFont(_richTextBox1.SelectionFont.ToString(), _richTextBox1.SelectionFont.Size, FontStyle.Regular, true);
                }
            }
        }
    
        bool _maskChanges;
    
        private void ChangeFontStyleForSelectedText(string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle)
        {
            _maskChanges = true;
            try
            {
                int txtStartPosition = _richTextBox1.SelectionStart;
                int selectionLength = _richTextBox1.SelectionLength;
                if (selectionLength > 0)
                    using (RichTextBox txtTemp = new RichTextBox())
                    {
                        txtTemp.Rtf = _richTextBox1.SelectedRtf;
                        for (int i = 0; i < selectionLength; ++i)
                        {
                            txtTemp.Select(i, 1);
                            txtTemp.SelectionFont = RenderFont(txtTemp.SelectionFont, familyName, emSize, fontStyle, enableFontStyle);
                        }
    
                        txtTemp.Select(0, selectionLength);
                        _richTextBox1.SelectedRtf = txtTemp.SelectedRtf;
                        _richTextBox1.Select(txtStartPosition, selectionLength);
                    }
            }
            finally
            {
                _maskChanges = false;
            }
        }
    
        private Font RenderFont(Font originalFont, string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle)
        {
            if (fontStyle.HasValue && fontStyle != FontStyle.Regular && fontStyle != FontStyle.Bold && fontStyle != FontStyle.Italic && fontStyle != FontStyle.Underline)
                throw new System.InvalidProgramException("Invalid style parameter to ChangeFontStyleForSelectedText");
    
            Font newFont;
            FontStyle? newStyle = null;
            if (fontStyle.HasValue)
            {
                if (fontStyle.HasValue && fontStyle == FontStyle.Regular)
                    newStyle = fontStyle.Value;
                else if (originalFont != null && enableFontStyle.HasValue && enableFontStyle.Value)
                    newStyle = originalFont.Style | fontStyle.Value;
                else
                    newStyle = originalFont.Style & ~fontStyle.Value;
            }
    
            newFont = new Font(!string.IsNullOrEmpty(familyName) ? familyName : originalFont.FontFamily.Name,
                                emSize.HasValue ? emSize.Value : originalFont.Size,
                                newStyle.HasValue ? newStyle.Value : originalFont.Style);
            return newFont;
        }
    
        private void ChangeOrSetFont(string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle)
        {
            if (_richTextBox1.SelectionType == RichTextBoxSelectionTypes.Empty)
            {
                SetSelectionFont(familyName, emSize, fontStyle, enableFontStyle);
            }
            else
            {
                ChangeFontStyleForSelectedText(familyName, emSize, fontStyle, enableFontStyle);
            }
        }
    
        private void SetSelectionFont(string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle)
        {
            Font renderedFont = RenderFont(_richTextBox1.SelectionFont, familyName, emSize, fontStyle, enableFontStyle);
            _richTextBox1.SelectionFont = renderedFont;
        }
    

    这是链接:

    http://how-to-code-net.blogspot.com/2014/01/how-to-make-custom-richtextbox-control.html

    【讨论】:

    • @maliyassi:我已经编辑了肯定可以工作的答案。
    • @maliyassi:它会使文本变粗吗?
    • 是的,但是当我尝试将字体样式恢复为常规时它不起作用。
    • @maliyassi:我将代码更改为一个非常有用的代码,其中包含很多控件。我也测试了它,它肯定会工作。
    【解决方案2】:

    问题出在这里:

    rtbMakale.SelectionFont = new Font(rtbMakale.SelectionFont,
                    FontStyle.Regular | rtbMakale.SelectionFont.Style);
    

    在这种情况下,SelectionFont.Style 包含标志 FontStyle.Bold。通过对旧样式使用 OR 运算符,所有现有样式(包括粗体)都将保留。

    改为使用逻辑运算符从样式中删除粗体标志:

    rtbMakale.SelectionFont = new Font(rtbMakale.SelectionFont,
        rtbMakale.SelectionFont.Style & ~FontStyle.Bold);
    

    将此部分rtbMakale.SelectionFont.Style &amp; ~FontStyle.Bold 阅读为按位运算“当前样式而不是粗体”


    一个更简单的方法是在旧样式上使用 XOR 运算符和要切换的新标志。这样就不用先检查样式了。

    private void btnBold_Click(object sender, EventArgs e)
    {
        if (rtbMakale.SelectionLength > 0)
        {
            rtbMakale.SelectionFont = new Font(rtbMakale.SelectionFont,
                rtbMakale.SelectionFont.Style ^ FontStyle.Bold);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      相关资源
      最近更新 更多