Fonts

G

Guest

I have three buttons in the toolbar; Bold, Italic, and Underline. When Bold and Italic are pressed, I want the selection font to be Bold and Italic. I chose this code, but it doesn't work

My Code

if(buttonbold.Pushed == true & buttonitalic.Pushed == true & buttonunderline.Pushed == false
{newfontstyle = FontStyle.Bold & FontStyle.Italic;

In Visual Basic .NET, I would use

newfontstyle = FontStyle.Bold + FontStyle.Itali

But that produces an error in C#. Please help me.
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Hi Bill,
I have three buttons in the toolbar; Bold, Italic, and Underline. When Bold and Italic are pressed, I want the selection font to be Bold and Italic. I chose this code, but it doesn't work.

My Code:

if(buttonbold.Pushed == true & buttonitalic.Pushed == true & buttonunderline.Pushed == false)

This if statement should not work in C# .. because of "&" operator
In C# boolean logic AND operator is represented as "&&".
{newfontstyle = FontStyle.Bold & FontStyle.Italic;}

You made an error here!
"&" (bitwise AND) operator with bit-orthogonal values allways returns
zero.
You should use "|" (bitwise OR) operator to set both bit-orthogonal
values.

{newfontstyle = FontStyle.Bold | FontStyle.Italic;}
In Visual Basic .NET, I would use:

newfontstyle = FontStyle.Bold + FontStyle.Italic

But that produces an error in C#. Please help me.

Regards

Marcin
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Hi Bill,
I have three buttons in the toolbar; Bold, Italic, and Underline. When Bold and Italic are pressed, I want the selection font to be Bold and Italic. I chose this code, but it doesn't work.

My Code:

if(buttonbold.Pushed == true & buttonitalic.Pushed == true & buttonunderline.Pushed == false)

This if statement should not work in C# .. because of "&" operator.
Boolean logic AND operator is represented as "&&" in C#.
{newfontstyle = FontStyle.Bold & FontStyle.Italic;}

You made an error here!
"&" (bitwise AND) operator with bit-orthogonal values allways returns
zero.
You should use "|" (bitwise OR) operator to set both bit-orthogonal
values.

{newfontstyle = FontStyle.Bold | FontStyle.Italic;}
In Visual Basic .NET, I would use:

newfontstyle = FontStyle.Bold + FontStyle.Italic

But that produces an error in C#. Please help me.

On the end, i can show You "better" way to set FontStyle:

newfontstyle =FontStyle.Regular;

if( buttonbold.Pushed ) {
newfontstyle=(newfontstyle | FontStyle.Bold);
}
if( buttonitalic.Pushed ) {
newfontstyle=(newfontstyle | FontStyle.Italic);
}
..
// and next font style
..

Regards

Marcin
 
M

Morten Wennevik

Hi Bill,


This if statement should not work in C# .. because of "&" operator
In C# boolean logic AND operator is represented as "&&".

Actually, both & and && is valid boolean operators. The difference being
if the first statement is false, & causes the next to be checked, while &&
will abort the if statement

int n = 0;
if(++n == 0 & ++n == 0){}

MessageBox.Show(n.ToString()); // shows 2

n = 0;
if(++n == 0 && ++n == 1){}

MessageBox.Show(n.ToString()); // shows 1

-->< snip ><--

Happy coding!
Morten Wennevik [C# MVP]
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Morten said:
Hi Bill,



This if statement should not work in C# .. because of "&" operator
In C# boolean logic AND operator is represented as "&&".

Actually, both & and && is valid boolean operators. The difference
being if the first statement is false, & causes the next to be checked,
while && will abort the if statement

int n = 0;
if(++n == 0 & ++n == 0){}

MessageBox.Show(n.ToString()); // shows 2

n = 0;
if(++n == 0 && ++n == 1){}

MessageBox.Show(n.ToString()); // shows 1

-->< snip ><--

Happy coding!
Morten Wennevik [C# MVP]

ups... :)

thanx for info!

Cheers!

Marcin
 

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