问题
I have a RTF box in my C# WinForms app.
Setting basic styles is fairly simple but when I try to unset the Italic style I lose all other applied styles to the selection font.
if (rtf.SelectionFont.Style.HasFlag(FontStyle.Italic))
{
rtf.SelectionFont = new Font(rtf.SelectionFont.FontFamily, rtf.SelectionFont.Size, rtf.SelectionFont.Style | FontStyle.Regular);
}
else
{
rtf.SelectionFont = new Font(rtf.SelectionFont.FontFamily, rtf.SelectionFont.Size, rtf.SelectionFont.Style | FontStyle.Italic);
}
Is there a way to just de-select the italic property without losing the Bold,underline ect.
回答1:
Use an XOR rather than an OR to remove a single FontStyle
- for example:
private void btnBold_Click(object sender, EventArgs e)
{
var currentStyle = rtf.SelectionFont.Style;
var newStyle =
rtf.SelectionFont.Bold ?
currentStyle ^ FontStyle.Bold :
currentStyle | FontStyle.Bold;
rtf.SelectionFont =
new Font(
rtf.SelectionFont.FontFamily,
rtf.SelectionFont.Size,
newStyle);
}
private void btnItalic_Click(object sender, EventArgs e)
{
var currentStyle = rtf.SelectionFont.Style;
var newStyle =
rtf.SelectionFont.Italic ?
currentStyle ^ FontStyle.Italic :
currentStyle | FontStyle.Italic;
rtf.SelectionFont =
new Font(
rtf.SelectionFont.FontFamily,
rtf.SelectionFont.Size,
newStyle);
}
With this implementation, removing the bold or italic style will not affect the other style if it is already applied to the selection.
BONUS:
For additional considerations like reselecting the selection after changing its style, an old DevX tip of the day might interest you too.
Also, the common logic in the style-specific handlers I offered begs to be factored out into a helper method that the style-specific handlers can leverage - e.g. private ChangeStyle(FontStyle style)
.
回答2:
You should try iterating over the enumeration and putting the style back together. Something like the following:
FontStyle oldStyle = rtf.SelectionFont.Style;
FontStyle newStyle = FontStyle.Regular;
foreach (Enum value in Enum.GetValues(oldStyle.GetType()))
{
if (oldStyle.HasFlag(value) && !value.Equals(FontStyle.Italic))
{
newStyle = newStyle | (FontStyle)value;
}
}
rtf.SelectionFont = new Font(rtf.SelectionFont.FontFamily, rtf.SelectionFont.Size, newStyle);
来源:https://stackoverflow.com/questions/21977157/set-unset-italic-in-richtextbox