Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# font Text event
I'm a beginner in programming and trying to teach myself at home using the Internet and such to practice on tasks and I'm trying to create a very simple word processor. With the font editing (Bold, underline, italic, etc.) I found using 
this.Font = new Font(this.Font, FontStyle.Underline);
changes the entire applications fonts to that single font style. I want the user to be able to have Bold and underlined at the same time so I tried using booleans. 
For example:
static bool bold = false;
static bool underlined = false;
private void label3_Click(object sender, EventArgs e)
{
            bold = true;
            fonts();
        }
        private void label2_Click(object sender, EventArgs e)
        {
            underlined = true;
            fonts();
        }
        void fonts()
        {
            if ((bold) && (underlined))
            {
                textBox1.Font = new Font(textBox1.Font, FontStyle.Bold & FontStyle.Underline);
                underlined = false;
                bold = false;
            }
        }
but nothing happens when the bold and underlined label are clicked when it should notice both bool's are true and therefore set textBox1.Font to bold and underlined, right?
 
But nothing happens. Why? Big Grin | :-D
Remember I'm very new to this and any basic and easy to understand explanations and tips would be so appreciated =)
Posted 7-Jan-12 11:42am
Edited 8-Jan-12 7:12am
v4
Comments
SAKryukov at 8-Jan-12 1:15am
   
What do you try to achieve by "FontStyle.Bold & FontStyle.Underline"? :-)
--SA
Joel Whatley- at 8-Jan-12 8:31am
   
Trying to make it bold and underlined :-) but it only allows one :-(
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

FontStyle is enum and therefore you can set multiple styles by |, like this:
 
textBox1.Font = new Font(textBox1.Font, FontStyle.Bold | FontStyle.Underline);
  Permalink  
Comments
SAKryukov at 8-Jan-12 1:16am
   
Most likely, that was the problem. My 5. OP is recommended to understand bitwise operations, and Boolean as well.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

1. Assuming that you are now using a TextBox: all changes to any TextBox Font property will set the font-styles, size, etc., for ALL content. If that's what you want, you may want to skip the rest of this comment Smile | :)
 
... edit : removed incorrect content: properties of a Font of a Control, like a TextBox, like 'Bold are read-only ...
 
a. looking at your specific code: why are you bothering to reset 'bold and 'underlined booleans to false ?
 
b. you already have good answers here to show you how to combine style flags using logical-Or, so that's taken care of
 
c. if you want to define FontStyles in advance and re-use them: it's pretty easy:
FontStyle fs1 = new FontStyle();
fs1 = FontStyle.Bold;
 
textBox1.Font = new Font(textBox1.Font, fs1);
 
FontStyle fs2 = new FontStyle();
fs2 = FontStyle.Underline;
 
textBox1.Font = new Font(textBox1.Font, fs1 | fs2);
2. But, assuming that you want to make a text-editor where some of the Text can be underlined, or bolded, but other parts of the Text can be plain, or in different font-sizes, or even colors:
 
You need to change to using a RichTextBox.
 
Here on CP are several excellent examples of extended RichTextBox based editors: suggest you search for them, and study them.
 
best, Bill
  Permalink  
v4
Comments
Joel Whatley- at 8-Jan-12 8:31am
   
Thanks a lot =) I will try out the rich text box and see if I can work out the font styles for a selection in the text in the textbox. What I really want is for the user to highlight some text in the textbox and be able to edit just that bit, instead of the whole textbox =)
thanks for help!
BillWoodruff at 8-Jan-12 10:25am
   
Hi, when you say "highlight" here, I am a little confused, because I usually think of selecting text in a Text Container Control as "high-lighting" it. And the other use of "highlight" I "naturally" think of, second, is like what happens when you do a "search" and you want all the results indicated in some visual way. But, here I think you may mean something more by the word "highlight."
 
When you say "edit just that bit:" again, to me, that's a little ambiguous: do you mean by "edit" to change the style (bold, italic, underline), or the color (foreground, or backcolor), or the font-size ? Or do you mean "edit" in the usual sense of changing the text content ?
 
The more exactly you tell us what you want to do, the more exactly we can give you feedback.
 
good luck, Bill
Joel Whatley- at 8-Jan-12 8:34am
   
Also this "textBox1.Font.Bold = true;" which you posted does not work for me :-( it says it's read only
BillWoodruff at 8-Jan-12 10:18am
   
Yes, Joel, you're absolutely right ... my bad ... I got carried away thinking about FontStyle. Will correct my response immediately.
Joel Whatley- at 9-Jan-12 12:10pm
   
Lol oke =) you helped me a lot anyway, thank you for that!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 378
1 OriginalGriff 230
2 DamithSL 200
3 Zoltán Zörgő 149
4 Dave Kreskowiak 120
0 OriginalGriff 7,610
1 DamithSL 5,559
2 Sergey Alexandrovich Kryukov 5,324
3 Maciej Los 4,961
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 8 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100