Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 16. Dialog Boxes > Choosing Fonts and Colors

16.10. Choosing Fonts and Colors

Let's take a look at both FontDialog and ColorDialog in a single program that lets you set the BackColor, ForeColor, and Font properties of a form.

FontAndColorDialogs.cs
//--------------------------------------------------
// FontAndColorDialogs.cs © 2001 by Charles Petzold
//--------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;

class FontAndColorDialogs:Form
{
     public static void Main()
     {
          Application.Run(new FontAndColorDialogs());
     }
     public FontAndColorDialogs()
     {
          Text = "Font and Color Dialogs";
          ResizeRedraw = true;

          Menu = new MainMenu();
          Menu.MenuItems.Add("&Format");
          Menu.MenuItems[0].MenuItems.Add("&Font...",
                                        new EventHandler(MenuFontOnClick));
          Menu.MenuItems[0].MenuItems.Add("&Background Color...",
                                        new EventHandler(MenuColorOnClick));
     }
     void MenuFontOnClick(object obj, EventArgs ea)
     {
          FontDialog fontdlg = new FontDialog();

          fontdlg.Font  = Font;
          fontdlg.Color = ForeColor;
          fontdlg.ShowColor = true;

          if(fontdlg.ShowDialog() == DialogResult.OK)
          {
               Font = fontdlg.Font;
               ForeColor  = fontdlg.Color;
               Invalidate();
          }
     }
     void MenuColorOnClick(object obj, EventArgs ea)
     {
          ColorDialog clrdlg = new ColorDialog();

          clrdlg.Color = BackColor;

          if (clrdlg.ShowDialog() == DialogResult.OK)
               BackColor = clrdlg.Color;
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics     grfx   = pea.Graphics;
          StringFormat strfmt = new StringFormat();

          strfmt.Alignment = strfmt.LineAlignment = StringAlignment.Center;

          grfx.DrawString("Hello common dialog boxes!", Font,
                          new SolidBrush(ForeColor),
                          this.ClientRectangle, strfmt);
     }
}

					  


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint