home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Menus / CheckAndRadioCheck / CheckAndRadioCheck.cs next >
Encoding:
Text File  |  2001-01-15  |  2.4 KB  |  78 lines

  1. //-------------------------------------------------
  2. // CheckAndRadioCheck.cs ⌐ 2001 by Charles Petzold
  3. //-------------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class CheckAndRadioCheck: Form
  9. {
  10.      MenuItem miColor, miFill;
  11.  
  12.      public static void Main()
  13.      {
  14.           Application.Run(new CheckAndRadioCheck());
  15.      }
  16.      public CheckAndRadioCheck()
  17.      {
  18.           Text = "Check and Radio Check";
  19.           ResizeRedraw = true;
  20.  
  21.           string[]     astrColor = {"Black", "Blue",    "Green",  "Cyan",
  22.                                     "Red",   "Magenta", "Yellow", "White"};
  23.           MenuItem[]   ami       = new MenuItem[astrColor.Length + 2];
  24.           EventHandler ehColor   = new EventHandler(MenuFormatColorOnClick);
  25.  
  26.           for (int i = 0; i < astrColor.Length; i++)
  27.           {
  28.                ami[i] = new MenuItem(astrColor[i], ehColor);
  29.                ami[i].RadioCheck = true;
  30.           }
  31.           miColor = ami[0];
  32.           miColor.Checked = true;
  33.  
  34.           ami[astrColor.Length] = new MenuItem("-");
  35.           
  36.           miFill = new MenuItem("&Fill",
  37.                                 new EventHandler(MenuFormatFillOnClick));
  38.  
  39.           ami[astrColor.Length + 1] = miFill;
  40.  
  41.           MenuItem mi = new MenuItem("&Format", ami);
  42.           
  43.           Menu = new MainMenu(new MenuItem[] {mi});
  44.      }
  45.      void MenuFormatColorOnClick(object obj, EventArgs ea)
  46.      {
  47.           miColor.Checked = false;
  48.           miColor = (MenuItem)obj;
  49.           miColor.Checked = true;
  50.  
  51.           Invalidate();
  52.      }
  53.      void MenuFormatFillOnClick(object obj, EventArgs ea)
  54.      {
  55.           MenuItem mi = (MenuItem)obj;
  56.  
  57.           mi.Checked ^= true;
  58.  
  59.           Invalidate();
  60.      }
  61.      protected override void OnPaint(PaintEventArgs pea)
  62.      {
  63.           Graphics grfx = pea.Graphics;
  64.  
  65.           if (miFill.Checked)
  66.           {
  67.                Brush brush = new SolidBrush(Color.FromName(miColor.Text));
  68.                grfx.FillEllipse(brush, 0, 0, ClientSize.Width - 1,
  69.                                              ClientSize.Height - 1);
  70.           }
  71.           else
  72.           {
  73.                Pen pen = new Pen(Color.FromName(miColor.Text));
  74.                grfx.DrawEllipse(pen, 0, 0, ClientSize.Width - 1,
  75.                                            ClientSize.Height - 1);
  76.           }
  77.      }
  78. }