FontStyle Question

S

Sammut

Hi,

I have started using the RichTextBox and got a bit stuck on the fontstyle
This is my current code

System.Drawing.Font fCurr = richTextBox1.SelectionFont;

System.Drawing.FontStyle nCurr = new FontStyle();



if (fCurr.Bold) nCurr = nCurr | FontStyle.Bold;

if (fCurr.Underline) nCurr = nCurr | FontStyle.Underline;

if (!(fCurr.Italic)) nCurr = nCurr | FontStyle.Italic;

is there any easier way to do this by getting the Current font style and add or remove what is needed



and also what does the "|" stand for and what is it's opposite if there is?



Thanks

Ivan Sammt
 
B

Bob Powell [MVP]

"|" is the operator for bitwise OR.it combines the bits with those in the target variable.

To reset the bits you have to use a bit-mask with the bits you want to zero set to zero. For example, to turn off bold you would do this...

nCurr=nCurr & (FontStyle.Bold ^ 0xffffffff)

#1-------------^^^^^^^^^^^^^^^^^^^^^^^^^^^ Flips the bits in the fontstyle
#2----------^ bitwise AND of the mask and the original

(view this in courier or any monospace font)

--
Bob Powell [MVP]
Visual C#, System.Drawing

Check out February's edition of Well Formed.
Non-client drawing, Graphics Transform stack and Flood-Filling
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

Hi,

I have started using the RichTextBox and got a bit stuck on the fontstyle
This is my current code

System.Drawing.Font fCurr = richTextBox1.SelectionFont;

System.Drawing.FontStyle nCurr = new FontStyle();



if (fCurr.Bold) nCurr = nCurr | FontStyle.Bold;

if (fCurr.Underline) nCurr = nCurr | FontStyle.Underline;

if (!(fCurr.Italic)) nCurr = nCurr | FontStyle.Italic;

is there any easier way to do this by getting the Current font style and add or remove what is needed



and also what does the "|" stand for and what is it's opposite if there is?



Thanks

Ivan Sammt
 

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