Exercises


Because the FontDialog and the ColorDialog work in a similar way to the other dialogs discussed in this chapter, it's an easy job to add these dialogs to the Simple Editor application.

  1. Let the user change the font of the text box. To make this possible add a new menu entry to the main menu: F&ormat, and a submenu for Format: &Font.... Add a handler to this menu item. Add a FontDialog to the application with the help of the Windows Forms designer. Display this dialog in the menu handler, and set the Font property of the text box to the selected font.

    You also have to change the implementation of the OnPrintPage() method to use the selected font for a printout. In the previous implementation you created a new Font object in the DrawString() method of the Graphics object. Now, use the font of the textBoxEdit object by accessing the Font property instead. You also have to be aware of a font location problem if the user chooses a big font. To avoid one line partly overwriting the one above/below, change the fixed value you used to change the vertical position of the lines. A better way to do this would be to use the size of the font to change the vertical increment: use the Height property of the Font class.

  2. Another great extension to the Simple Editor application would be to change the font color. Add a second submenu to the Format menu entry: Color.... Add a handler to this menu entry where you open up a ColorDialog. If the user presses the OK button, set the selected color of the ColorDialog to the ForeColor property of the text box.

    In the OnPrintPage() method make sure that the chosen color is used only if the printer supports colors. You can check the color support of the printer with the PageSettings.Color property of the PrintPageEventArgs argument. You can create a brush object with the color of the text box with this code:

     Brush brush = new SolidBrush(textBoxEdit.ForeColor); 

    This brush can then be used as an argument in the DrawString() method instead of the black brush you used in the example before.




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net