Color Fill dialog : Dialog « GUI Windows Form « C# / C Sharp






Color Fill dialog

  

using System;
using System.Drawing;
using System.Windows.Forms;
   
class ColorFillDialogBox: Form
{
     GroupBox grpbox = new GroupBox();
     CheckBox chkbox = new CheckBox();
   
     public ColorFillDialogBox()
     {
          string[] astrColor = { "Black", "Blue", "Green", "Cyan",   
                                 "Red", "Magenta", "Yellow", "White"};
   
          grpbox.Parent   = this;
          grpbox.Text     = "Color";
          grpbox.Location = new Point(8, 8);
          grpbox.Size     = new Size(96, 12 * (astrColor.Length + 1));
   
          for (int i = 0; i < astrColor.Length; i++)
          {
               RadioButton radiobtn = new RadioButton();
               radiobtn.Parent      = grpbox;
               radiobtn.Text        = astrColor[i];
               radiobtn.Location    = new Point(8, 12 * (i + 1));
               radiobtn.Size        = new Size(80, 10);
          }
          chkbox.Parent   = this;
          chkbox.Text     = "Fill Ellipse";
          chkbox.Location = new Point(8, grpbox.Bottom + 4);
          chkbox.Size     = new Size(80, 10);
   
          Button btn   = new Button();
          btn.Parent   = this;
          btn.Text     = "OK";
          btn.Location = new Point(8, chkbox.Bottom + 4);
          btn.Size     = new Size(40, 16);
          btn.DialogResult = DialogResult.OK;
          AcceptButton = btn;
   
          btn  = new Button();
          btn.Parent   = this;
          btn.Text     = "Cancel";
          btn.Location = new Point(64, chkbox.Bottom + 4);
          btn.Size     = new Size(40, 16);
          btn.DialogResult = DialogResult.Cancel;
          CancelButton = btn;
   
          ClientSize = new Size(112, btn.Bottom + 8);
          AutoScaleBaseSize = new Size(4, 8);
     }
     public Color Color
     {
          get 
          { 
               for (int i = 0; i < grpbox.Controls.Count; i++)
               {
                    RadioButton radiobtn = (RadioButton) grpbox.Controls[i];
                    if (radiobtn.Checked)
                         return Color.FromName(radiobtn.Text);
               }
               return Color.Black;
               
          }  
          set 
          { 
               for (int i = 0; i < grpbox.Controls.Count; i++)
               {
                    RadioButton radiobtn = (RadioButton) grpbox.Controls[i];
   
                    if (value == Color.FromName(radiobtn.Text))
                    {
                         radiobtn.Checked = true;
                         break;
                    }
               }
          }
     }
     public bool Fill
     {
          get { return chkbox.Checked; }
          set { chkbox.Checked = value; }
     }
}

   
class DrawOrFillEllipse: Form
{
     Color colorEllipse = Color.Red;
     bool  bFillEllipse = false;
   
     public static void Main()
     {
          Application.Run(new DrawOrFillEllipse());
     }
     public DrawOrFillEllipse()
     {
          ResizeRedraw = true;
          Menu = new MainMenu();
          Menu.MenuItems.Add("&Options");
          Menu.MenuItems[0].MenuItems.Add("&Color...", new EventHandler(MenuColorOnClick));
     }
     void MenuColorOnClick(object obj, EventArgs ea)
     {
          ColorFillDialogBox dlg = new ColorFillDialogBox();
   
          dlg.Color = colorEllipse;
          dlg.Fill  = bFillEllipse;
   
          if (dlg.ShowDialog() == DialogResult.OK)
          {
               colorEllipse = dlg.Color;
               bFillEllipse = dlg.Fill;
               Invalidate();
          }
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics  grfx = pea.Graphics;
          Rectangle rect = new Rectangle(0, 0, 50, 50);
          if(bFillEllipse)
               grfx.FillEllipse(new SolidBrush(colorEllipse), rect);
          else
               grfx.DrawEllipse(new Pen(colorEllipse), rect);
     }
}

   
  








Related examples in the same category

1.A dialog by user defined property
2.Color Scroll Dialog Box
3.Italic User Message Dialog: your own dialog
4.Use DialogResult property in Form
5.Define your own dialog box and get user inputDefine your own dialog box and get user input
6.Microsoft.Win32.OpenFileDialog/SaveFileDialog
7.Get a list of files from a folder based on the result of a FolderBrowserDialog