5 Replies - 310 Views - Last Post: 01 December 2011 - 09:32 AM Rate Topic: -----

#1 ATC3  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 04-October 11

Problem with text style

Posted 30 November 2011 - 09:41 AM

Okay, I am making a word processor. Keep in mind that I am a beginner at C#. Thus said, here is my problem. I have a toolstrip the keeps 3 buttons in my Word processor. They are buttons for italics, bold and Underline. I have got them working. In fact, here is my code:

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (richTextBox1.SelectionFont != null)
            {
                System.Drawing.Font currentFont = richTextBox1.SelectionFont;
                System.Drawing.FontStyle newFontStyle;

                if (richTextBox1.SelectionFont.Bold == true)
                {
                    newFontStyle = FontStyle.Regular;
                }
                else
                {
                    newFontStyle = FontStyle.Bold;
                }

                richTextBox1.SelectionFont = new Font(
                    currentFont.FontFamily,
                    currentFont.Size,
                    newFontStyle
                    );
            }
        }


Now the problem is that when I already have a font style selected it will change it and only use the new font style. How do I make it use both, or all of the font styles selected? I was thinking an if statement. However since I am a beginner I need help. I would really appreciate it if someone would give me the necessary code.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with text style

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5464
  • View blog
  • Posts: 11,731
  • Joined: 02-June 10

Re: Problem with text style

Posted 30 November 2011 - 09:49 AM

Let me throw in a couple tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.

  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    Spoiler

Was This Post Helpful? 1
  • +
  • -

#3 ATC3  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 04-October 11

Re: Problem with text style

Posted 30 November 2011 - 09:57 AM

Okay, I greatly appreciate the tips. However I am just beginning and need help. I know my coding habits are probably sloppy. But I am young and very inexperienced. I just started coding 4 months ago. So any help on this problem would be appreciated. I will admit that the code is bad, but it is how it is. So can anyone help me with this problem?
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5464
  • View blog
  • Posts: 11,731
  • Joined: 02-June 10

Re: Problem with text style

Posted 30 November 2011 - 10:10 AM

I try to throw those out ESPECIALLY when someone is just getting into coding. It is so much easier to start with good habits than to un-learn bad habits and retrain yourself later.

Doing what you want is done through bitwise addition. There is an article here for doing exactly this with font styles.

Personally I would keep some bool values for flags.
IsItalicOn
IsBoldOn

and so on.. Then toggle them on and off when someone clicks the button in the toolbar, resulting in call to the method that updates the font style.

That way you can code with values that are easy to understand. Long term maintainability!
Was This Post Helpful? 1
  • +
  • -

#5 ATC3  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 04-October 11

Re: Problem with text style

Posted 30 November 2011 - 10:12 AM

View PosttlhIn`toq, on 30 November 2011 - 10:10 AM, said:

I try to throw those out ESPECIALLY when someone is just getting into coding. It is so much easier to start with good habits than to un-learn bad habits and retrain yourself later.

Doing what you want is done through bitwise addition. There is an article here for doing exactly this with font styles.

Personally I would keep some bool values for flags.
IsItalicOn
IsBoldOn

and so on.. Then toggle them on and off when someone clicks the button in the toolbar, resulting in call to the method that updates the font style.

That way you can code with values that are easy to understand. Long term maintainability!

I love your response. Thank you very much. I will carry these tips with me for a long time. I appreciate your help. It is a shame you can only give out 1 reputation point for each post. I wish it was more.
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4450
  • View blog
  • Posts: 7,748
  • Joined: 08-June 10

Re: Problem with text style

Posted 01 December 2011 - 09:32 AM

Also, here's an excellent article about using enums and bitmasks:

http://msdn.microsof...um.hasflag.aspx

Actually, it shows how to use the Enum.HasFlag method, but it still has great examples.

This article can help as well:

http://msdn.microsof...sattribute.aspx
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1