Winforms RichtextBox粗体/斜体/下划线格式问题

Winforms RichtextBox粗体/斜体/下划线格式问题,winforms,formatting,richtextbox,Winforms,Formatting,Richtextbox,从标题可以看出,我在RichTexBox控件中为选定文本指定和删除格式样式方面有点问题 我知道如何将文本单独设置为粗体/斜体/下划线,但不能将它们组合在一起。我知道可以一个字符一个字符地实现这一点的方法,但在界面上这似乎很耗时。如果它可以毫不费力地在写字板上完成,我相信它可以在这里实现 没有这样或那样的方法可以让我从RichTextBox.SelectedFont?中“添加”或“删除”样式,除非我完全误解了这个问题 // Get the current text selection or to

从标题可以看出,我在RichTexBox控件中为选定文本指定和删除格式样式方面有点问题

我知道如何将文本单独设置为粗体/斜体/下划线,但不能将它们组合在一起。我知道可以一个字符一个字符地实现这一点的方法,但在界面上这似乎很耗时。如果它可以毫不费力地在写字板上完成,我相信它可以在这里实现


没有这样或那样的方法可以让我从RichTextBox.SelectedFont

中“添加”或“删除”样式,除非我完全误解了这个问题

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

我必须和你一样思考。我看这是一个老帖子。然而,对于那些可能遇到相同问题的人。除非逐个字符进行迭代,从而获得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;
    }
//
///将字体从原始字体更改为附加其他属性
/// 
///文本的原始字体
///目标姓氏
///目标文本大小
///目标字体样式
///启用时为true禁用时为false
///将所有提供的属性添加/删除到原始字体的新字体
私有字体RenderFont(字体原始字体、字符串familyName、float?emSize、FontStyle?FontStyle、bool?enableFontStyle)
{
如果(fontStyle.HasValue&&fontStyle!=fontStyle.Regular&&fontStyle!=fontStyle.Bold&&fontStyle!=fontStyle.Italic&&fontStyle!=fontStyle.Underline)
抛出新System.InvalidProgrameException(“将无效样式参数转换为ChangeFontStyleForSelectedText”);
字体新建字体;
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;
其他的
newStyle=originalFont.Style&~fontStyle.Value;
}
newFont=新字体(!string.IsNullOrEmpty(familyName)?familyName:originalFont.FontFamily.Name,
emSize.HasValue?emSize.Value:originalFont.Size,
newStyle.HasValue?newStyle.Value:originalFont.Style);
返回新字体;
}

有关如何制作自定义richtexBox控件的更多详细信息,请转到

Hey!谢谢你的回复!我还没有尝试过这个,虽然这看起来很适合添加样式,但是如何从一个选择的字体中删除,比如说“粗体”,它的样式组合为粗体、下划线和斜体?哦,等等……理想情况下,这两种字体都可以使用!让我试试这个,然后回到美国删除粗体。。。获取当前字体。它以一种不在大胆和重新分配。