Changing the Font to Bold

H

Henry Jones

I found some code to change the font on a button to bold:

private void btnBold_Click(object sender, System.EventArgs e)

{

btnCalculate.Font = new Font(btnCalculate.Font,

btnCalculate.Font.Style | FontStyle.Bold);

}



everything is cool. It works. I then wanted to change the font back to
normal:



private void btnNormal_Click(object sender, System.EventArgs e)

{

this.btnCalculate.Font = new Font(btnCalculate.Font,

btnCalculate.Font.Style | FontStyle.Regular);

}



It doesn't work. Why can't I change the font back to Normal?



Thank you.
 
J

Jianwei Sun

Henry said:
I found some code to change the font on a button to bold:

private void btnBold_Click(object sender, System.EventArgs e)

{

btnCalculate.Font = new Font(btnCalculate.Font,

btnCalculate.Font.Style | FontStyle.Bold);

}



everything is cool. It works. I then wanted to change the font back to
normal:



private void btnNormal_Click(object sender, System.EventArgs e)

{

this.btnCalculate.Font = new Font(btnCalculate.Font,

btnCalculate.Font.Style | FontStyle.Regular);

}



It doesn't work. Why can't I change the font back to Normal?



Thank you.

In the statement:

btnCalculate.Font = new Font(btnCalculate.Font
btnCalculate.Font.Style| FontStyle.Bold);

you are using "|" operation btnCalculate.Font.Style| FontStyle.Bold,
which added the Bold style of the original style ( I assume it is normal).

And then again, in the second function, this.btnCalculate.Font = new
Font(btnCalculate.Font, btnCalculate.Font.Style | FontStyle.Regular);
you are doing another "|" operation, which takes no effect. Since
Font.Style is already FontStyle.Regular|FontStyle.Bold.

I will do this instead:
private void btnBold_Click(object sender, System.EventArgs e)
{
btnCalculate.Font = new Font(btnCalculate.Font, FontStyle.Bold);

}
private void btnNormal_Click(object sender, System.EventArgs e)

{

this.btnCalculate.Font = new Font(btnCalculate.Font,FontStyle.Regular);

}

Jianwei
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top