【问题标题】:Winforms RichtextBox Bold/Italic/Underline Formatting issueWinforms RichtextBox 粗体/斜体/下划线格式问题
【发布时间】:2017-01-01 03:30:45
【问题描述】:

从标题可以看出,我在为 RichTexBox 控件中的选定文本分配和删除格式样式方面存在一些问题。

我知道如何单独制作粗体/斜体/下划线文本,但不是这些的组合。我知道可以逐个字符实现此字符的方法,但这在界面上似乎很耗时。如果可以在写字板中轻松完成,我相信在这里可以实现!

是否不存在可以让我从 RichTextBox.SelectedFont 中“添加”或“删除”样式的方法?

【问题讨论】:

    标签: winforms formatting richtextbox


    【解决方案1】:

    除非我完全误解了这个问题

    // Get the current text selection or to text entered after the insertion point. 
    // Build new font based on the selection font, make it both Bold and Underline
    // Apply new font to currently selected text (or for new text at insertion point
    
    Font currFont = richTextBox.SelectionFont;
    Font boldUnderFont = new Font(currFont, FontStyle.Bold | FontStyle.Underline);
    richTextBox.SelectionFont = boldUnderFont;
    

    【讨论】:

    • 嘿!谢谢回复!我还没有尝试过,虽然这看起来很适合添加样式,但你如何从具有粗体、下划线和斜体样式组合的选定字体中删除,说“粗体”?
    • 哦,等等……理想情况下,这两者都可以使用!让我试一试,然后回复你。
    • 删除粗体...获取当前字体。并且在粗体上加一个 NOT 并重新分配。
    【解决方案2】:

    我必须做和你一样的想法。我看到这是一个旧帖子。但是,对于那些可能遇到相同问题的人。您不能将字体样式、字体系列...应用于字符串,除非您逐个字符地迭代,因此您可以获得 SelectionFont。 这是可以帮助您的方法:

    /// <summary>
        /// Changes a font from originalFont appending other properties
        /// </summary>
        /// <param name="originalFont">Original font of text
        /// <param name="familyName">Target family name
        /// <param name="emSize">Target text Size
        /// <param name="fontStyle">Target font style
        /// <param name="enableFontStyle">true when enable false when disable
        /// <returns>A new font with all provided properties added/removed to original font</returns>
        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;
        }
    

    有关如何制作自定义richtexBox控件的更多详细信息,您可以去http://how-to-code-net.blogspot.ro/2014/01/how-to-make-custom-richtextbox-control.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-25
      • 1970-01-01
      • 2012-07-20
      • 2013-11-06
      相关资源
      最近更新 更多