Mixinf fonts in a TextBox?

Z

zacks

I have a multiline textbox control where I would like to load up with
something like the following:

This is Line 1 <- this line is NOT bold
This is Line 2 <- this line is bold
This is Line 3 <- this line is NOT bold

I have attempted to use C# code like the following:

string part1 = "This is Line 1" + Environment.NewLine;
string part2 = "This is Line 2" + Environment.NewLine;
string part3 = "This is Line 3" + Environment.NewLine;

textbox.Text = part1;
textbox.Font = new Font(textbox.Font, textbox.Font.Style |
FontStyle.Bold);
textbox.AppendText(part2);
textbox.Font = new Font(textbox.Font, textbox.Font.Style | !
FontStyle.Bold);
textbox.AppendText(part3);

But the compiler complains that the ! operator cannot appear where it
is. I know there has to be some simple way to do this, but I just
haven't been able to find it yet.

Can anyone help?

TIA,
 
T

Teemu

I have a multiline textbox control where I would like to load up with
something like the following:

This is Line 1 <- this line is NOT bold
This is Line 2 <- this line is bold
This is Line 3 <- this line is NOT bold

I can't answer your question about !-character but normal TextBox control
can have only one font. You should use RichTextBox in order to be able to
use different fonts.

-Teemu
 
P

Peter Morris

You need to use RichTextBox instead. I'm sure Google will be able to tell
you what to do with it.
 
T

Tom Dacon

You won't do it with a TextBox - use a RichTextBox instead.

Tom Dacon
Dacon Software Consulting
 
H

Herfried K. Wagner [MVP]

I have a multiline textbox control where I would like to load up with
something like the following:

This is Line 1 <- this line is NOT bold
This is Line 2 <- this line is bold
This is Line 3 <- this line is NOT bold

I have attempted to use C# code like the following:

string part1 = "This is Line 1" + Environment.NewLine;
string part2 = "This is Line 2" + Environment.NewLine;
string part3 = "This is Line 3" + Environment.NewLine;

textbox.Text = part1;
textbox.Font = new Font(textbox.Font, textbox.Font.Style |
FontStyle.Bold);
textbox.AppendText(part2);
textbox.Font = new Font(textbox.Font, textbox.Font.Style | !
FontStyle.Bold);
textbox.AppendText(part3);

Take a look at the RichTextBox control and its 'Select' method and
'SelectionFont' property.
 
P

Peter Duniho

[...]
textbox.Font = new Font(textbox.Font, textbox.Font.Style |
!FontStyle.Bold);
textbox.AppendText(part3);

But the compiler complains that the ! operator cannot appear where it
is. I know there has to be some simple way to do this, but I just
haven't been able to find it yet.

Even if the compiler allowed you to use ! there, the entire expression is
not what you'd want. The ! operator would presumably invert all of the
bits in the flags. "Or"-ing that with the previous value would result in
everything being enabled _except_ bold.

Rather, you want "textbox.Font.Style & ~FontStyle.Bold". This does a
bit-wise "not" (or inversion), and the uses that as a mask, "and"-ed with
the original value, with the result that any bits set before are still set
_except_ the bit representing bold.

You got a lot of people tell you that you need RichTextBox instead of
TextBox. There's nothing in your code that suggests to me that you're
using the wrong text box class, but if you're not using RichTextBox, they
are right...you'll need to.

Pete
 
Z

zacks

[...]
textbox.Font = new Font(textbox.Font, textbox.Font.Style |  
!FontStyle.Bold);
textbox.AppendText(part3);
But the compiler complains that the ! operator cannot appear where it
is. I know there has to be some simple way to do this, but I just
haven't been able to find it yet.

Even if the compiler allowed you to use ! there, the entire expression is  
not what you'd want.  The ! operator would presumably invert all of the  
bits in the flags.  "Or"-ing that with the previous value would result in  
everything being enabled _except_ bold.

Rather, you want "textbox.Font.Style & ~FontStyle.Bold".  This does a  
bit-wise "not" (or inversion), and the uses that as a mask, "and"-ed with  
the original value, with the result that any bits set before are still set 
_except_ the bit representing bold.

You got a lot of people tell you that you need RichTextBox instead of  
TextBox.  There's nothing in your code that suggests to me that you're  
using the wrong text box class, but if you're not using RichTextBox, they  
are right...you'll need to.

Pete

Thanks for all the responses. I knew that there was such a thing as a
rich text box, but had never had to do what I need to do now, and had
never looked into what they were or used for. I get it now.

And yes, I tried the ~ operator with no results, obviously, since I
was still using the text box then.
 

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