简体   繁体   English

如何为C#Winform一起设置粗体,斜体和下划线

[英]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.

相关问题 不能在 C# 中同时“添加”粗体和斜体 - Cannot “add” bold and italic together in C# Pdf锋利的字体样式大胆,斜体和下划线在一起 - Pdf sharp font style Bold,Italic and Underline together 需要使用javascript将选定的文本设置为粗体/斜体/下划线,并使用c#保存和检​​索相同的文本 - Need to make selected text as bold/italic/underline using javascript, and also save & retrieve the same using c# 如何以粗体,斜体,下划线和其他html标签阅读文本 - how to read text in bold,italic,underline and other html tags C# 或 VB 文档注释中的粗体或斜体? - Bold or italic in C# or VB documentation comments? 如何使用C#以不同的颜色,大小,字体,粗体和斜体格式以excel或csv格式写入数据 - How to write data in excel or csv with different color, size , Font , Bold and Italic format using c# 使用C#从Word文档中读取粗体和斜体字 - Read words that are bold and Italic from word document using c# 如何在 Gtk# 中做粗体、斜体、下划线按钮,又名如何在 gtk 中动态格式化文本 - How to do Bold, Italic, Underline buttons in Gtk# aka How to dynamically format text in gtk C#WinForm-使用立即DateTime和标签颜色在​​日期中使用斜体 - C# WinForm - Italic in Date using DateTime Now and Color Of Label 如何停止我的粗体/斜体/下划线选项将文本的字体更改回默认字体和大小 - How to stop my bold/italic/underline options changing the text's font back to the default font and size
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM