Form graphics not set when form loads

Posted by Jimmy on Stack Overflow See other posts from Stack Overflow or by Jimmy
Published on 2010-04-21T14:57:33Z Indexed on 2010/04/21 15:23 UTC
Read the original article Hit count: 203

Filed under:
|
|
|

My form has a group box which contains two overlapping rectangles. The form's other controls are two sets of four numeric up down controls to set the rectangles' colors. (nudF1,2,3 and 4 set the rectangle that's in front, and nudB1,2,3 and 4 set the rectangle that's behind.) Everything works fine, except that the rectangles do not display the colors set in the numeric up downs when the form first loads. The numeric up down controls' ChangeValue events all call the ShowColors() method. The form's Load event calls the csColorsForm_Load() method. Any suggestions?

namespace csColors
{
    public partial class csColorsForm : Form
    {
        public csColorsForm()
        {
            InitializeComponent();
        }

        private void csColorsForm_Load(object sender, EventArgs e)
        {
            this.BackColor = System.Drawing.Color.DarkBlue;
            SetColors(sender, e);
        }

        private void SetColors(object sender, EventArgs e)
        {
            Control control = (Control)sender;
            String ctrlName = control.Name;
            Graphics objGraphics;
            Rectangle rect1, rect2;
            int colorBack, colorFore;
            objGraphics = this.grpColor.CreateGraphics();
            // If calling control is not a forecolor control, paint backcolor rectangle
            if (ctrlName.Substring(0,4)!="nudF")
            {
                colorBack = int.Parse(SetColorsB("nudB"), NumberStyles.HexNumber);
                SolidBrush BrushB = new SolidBrush(Color.FromArgb(colorBack));
                rect1 = new Rectangle(this.grpColor.Left, this.grpColor.Top,
                    this.grpColor.Width, this.grpColor.Height);
                objGraphics.FillRectangle(BrushB, rect1);
            }
            // Always paint forecolor rectangle
            colorFore = int.Parse(SetColorsB("nudF"), NumberStyles.HexNumber);
            SolidBrush BrushF = new SolidBrush(Color.FromArgb(colorFore));
            rect2 = new Rectangle(this.grpColor.Left, this.grpColor.Top,
                this.grpColor.Width, this.grpColor.Height);
            objGraphics.FillRectangle(BrushF, rect2);
            objGraphics.Dispose();
        }

        private string SetColorsB(string nam)
        {
            string txt="";
            for (int n = 1; n <= 4; ++n)
            {
                var ud = Controls[nam + n] as NumericUpDown;
                int hex = (int)ud.Value;
                txt += hex.ToString("X2");
            }
            return txt;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about winforms