Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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 
C#
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:
C#
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? :D
Remember I'm very new to this and any basic and easy to understand explanations and tips would be so appreciated =)
Posted
Updated 8-Jan-12 6:12am
v4
Comments
Sergey Alexandrovich Kryukov 8-Jan-12 1:15am    
What do you try to achieve by "FontStyle.Bold & FontStyle.Underline"? :-)
--SA
Joel Whatley- 8-Jan-12 8:31am    
Trying to make it bold and underlined :-) but it only allows one :-(

FontStyle is enum and therefore you can set multiple styles by |, like this:

C#
textBox1.Font = new Font(textBox1.Font, FontStyle.Bold | FontStyle.Underline);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 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
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 :)

... 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
 
Share this answer
 
v4
Comments
Joel Whatley- 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 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- 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 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- 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900