[英]How to set bold, italic, underline together for C# Winform
Halo guys, i get a problem when I try to make program in C# winForm 晕,我尝试在C#winForm中制作程序时遇到问题
I have make 3 button (btn_bold, btn_italic, btn_underline), when i code my btn_bold with 当我用btn_bold编写代码时,我有3个按钮(btn_bold,btn_italic,btn_underline)
if (rTb_Isi.SelectionFont != null)
{
System.Drawing.Font currentFont = rTb_Isi.SelectionFont;
System.Drawing.FontStyle newFontStyle;
if (rTb_Isi.SelectionFont.Bold == true)
{
newFontStyle = FontStyle.Regular;
}
else
{
newFontStyle = FontStyle.Bold;
}
rTb_Isi.SelectionFont = new Font(
currentFont.FontFamily,
currentFont.Size,
newFontStyle
);
}
}
The problem is, when i click btn_bold, then italic text become bold, can't be bold and italic. 问题是,当我单击btn_bold时,斜体文本变为粗体,不能为粗体和斜体。
Do you know, how to make this code can work together like Ms. Word ? 您知道吗,如何使此代码可以像Word女士一样工作?
I have try to change the code be 我试图将代码更改为
if (rTb_Isi.SelectionFont != null)
{
System.Drawing.Font currentFont = rTb_Isi.SelectionFont;
System.Drawing.FontStyle newFontStyle;
if (rTb_Isi.SelectionFont.Bold == true)
{
newFontStyle = FontStyle.Regular;
}
else if (rTb_Isi.SelectionFont.Italic == true)
{
newFontStyle = FontStyle.Bold & FontStyle.Italic;
}
else
{
newFontStyle = FontStyle.Bold;
}
rTb_Isi.SelectionFont = new Font(
currentFont.FontFamily,
currentFont.Size,
newFontStyle
);
}
}
but it doesn't work :( 但这不起作用:(
The FontStyle enum has the [Flags] attribute. FontStyle枚举具有[Flags]属性。 That makes it very simple:
这非常简单:
System.Drawing.FontStyle newFontStyle = FontStyle.Regular;
if (rTb_Isi.SelectionFont.Bold) newFontStyle |= FontStyle.Bold;
if (rTb_Isi.SelectionFont.Italic) newFontStyle |= FontStyle.Italic;
if (rTb_Isi.SelectionFont.Underline) newFontStyle |= FontStyle.Underline;
if (newFontStyle != rTb_Isi.SelectionFont.Style) {
rTb_Isi.SelectionFont = new Font(currentFont.FontFamily, currentFont.Size, newFontStyle);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.