简体   繁体   English

更改字体样式

[英]Change Font Style

I have a label with text in Bold and Italic .我有一个带有BoldItalic文本的标签。 I want to change those font properties through a button click.我想通过单击按钮来更改这些字体属性。

I got to know of the code Label1.Font = new Font(Label1.Font, FontStyle.Regular);我知道了代码Label1.Font = new Font(Label1.Font, FontStyle.Regular);

But from this code it will undo both BOLD & ITALIC properties.但是从这段代码中,它将撤消BOLDITALIC属性。 I want only to remove bold property.....我只想删除粗体属性.....

Are there anything like fontsyle.bold = false ?有没有类似fontsyle.bold = false东西?

在创建新字体时使用原始字体的Font.Style ,使用& ~翻转样式

   label1.Font = new Font(label1.Font, label1.Font.Style & ~FontStyle.Bold);

You can try this also -- 你也可以尝试这个 -

label1.Font = new Font("Arial", 24,FontStyle.Bold);

or 要么

mainForm.lblName.Font = new Font("Arial", mainForm.lblName.Font.Size);

The constructor takes different parameters. 构造函数采用不同的参数。 see more 查看更多

The best option is to use bitcodes and the XOR operator ^ 最好的选择是使用bitcodes和XOR运算符^

try this code: 试试这段代码:

Label1.Font = new Font(Label1.Font.Style ^ FontStyle.Regular);
private void btn_bold_CheckedChanged(object sender, EventArgs e)
    {
        label1.Font = label2.Font = new Font( label1.Font,label1.Font.Style ^ FontStyle.Bold);
    }

    private void btn_italic_Click(object sender, EventArgs e)
    {
        label1.Font = label2.Font = new Font(label1.Font, label1.Font.Style ^ FontStyle.Italic);
    }

    private void btn_underline_Click(object sender, EventArgs e)
    {
        label1.Font = label2.Font = new Font(label1.Font, label1.Font.Style ^ FontStyle.Underline);
    }

This will do all what do u want这将做你想做的一切

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM