Changing the Font to Bold

  • Thread starter Thread starter Henry Jones
  • Start date Start date
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.
 
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
 
Back
Top