Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Mixinf fonts in a TextBox?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
zacks@construction-imaging.com - 09 Mar 2008 18:46 GMT
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,
Teemu - 09 Mar 2008 19:05 GMT
>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
Peter Morris - 09 Mar 2008 19:07 GMT
You need to use RichTextBox instead.  I'm sure Google will be able to tell
you what to do with it.
Tom Dacon - 09 Mar 2008 19:08 GMT
You won't do it with a TextBox - use a RichTextBox instead.

Tom Dacon
Dacon Software Consulting

>I have a multiline textbox control where I would like to load up with
> something like the following:
[quoted text clipped - 24 lines]
>
> TIA,
Herfried K. Wagner [MVP] - 09 Mar 2008 19:49 GMT
<zacks@construction-imaging.com> schrieb:
>I have a multiline textbox control where I would like to load up with
> something like the following:
[quoted text clipped - 16 lines]
> FontStyle.Bold);
> textbox.AppendText(part3);

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

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>

Peter Duniho - 09 Mar 2008 23:24 GMT
> [...]
> textbox.Font = new Font(textbox.Font, textbox.Font.Style |  
[quoted text clipped - 4 lines]
> 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
zacks@construction-imaging.com - 10 Mar 2008 13:41 GMT
On Mar 9, 6:24 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> > [...]
> > textbox.Font = new Font(textbox.Font, textbox.Font.Style |  
[quoted text clipped - 21 lines]
>
> 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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.