Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为C#Winform设置粗体、斜体和下划线_C#_Winforms - Fatal编程技术网

如何为C#Winform设置粗体、斜体和下划线

如何为C#Winform设置粗体、斜体和下划线,c#,winforms,C#,Winforms,Halo伙计们,当我试图用C#winForm编写程序时,我遇到了一个问题 我有3个按钮(btn_粗体,btn_斜体,btn_下划线),当我编码我的btn_粗体时 if (rTb_Isi.SelectionFont != null) { System.Drawing.Font currentFont = rTb_Isi.SelectionFont; System.Drawing.FontStyle newFontStyle;

Halo伙计们,当我试图用C#winForm编写程序时,我遇到了一个问题

我有3个按钮(btn_粗体,btn_斜体,btn_下划线),当我编码我的btn_粗体时

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
            );
        }
    }
问题是,当我点击btn_粗体时,斜体文本变为粗体,不能是粗体和斜体

你知道吗,如何使这个代码可以像Word女士一样协同工作

我已尝试将代码更改为

            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
            );
        }
    }

但它不起作用:(

FontStyle枚举具有[Flags]属性。这使得它非常简单:

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);
}

使用|而不是-,因为您想要组合比特。这可能会对您有所帮助