articles

Home / DeveloperSection / Articles / How to split form background color in C Sharp

How to split form background color in C Sharp

How to split form background color in C Sharp

Anonymous User 15473 02-Aug-2010

How to split form background color in C Sharp

This will change Windows form back color according to Button Click.

How to split form background color in C Sharp

Controls Name

Button Red: btnRed

                Blue: btnBlue

                Split: btnSplit

                Reset: btnReset

Code
   bool check; //boolean variable to check whether split is clicked or not.
//function to split form background color
  private void fnSplit()
        {           
            Graphics surface = this.CreateGraphics();
surface.FillRectangle(new SolidBrush(Color.Red), 0, 0, (this.Width) / 2, this.Height);
//drawing filled rectangle of red color on form left half side with width equal to half of form
//width andheigth equalto the height of form.
surface.FillRectangle(new SolidBrush(Color.Blue), (this.Width) / 2, 0, (this.Width) / 2, this.Height);
//drawing another rectanfle of blue color on other hand of the form of same dimension as that of firstrectangle.
        }
 
//changing form background color to red on Red button click
        private void btnRed_Click(object sender, EventArgs e)
        {
            this.BackColor = Color.Red;
//setting background color to red.
            check = false;          
        }

How to split form background color in C Sharp

//changing form background color to blue on Blue button click
        private void btnBlue_Click(object sender, EventArgs e)
        {
            this.BackColor = Color.Blue;
//setting background color to red.
            check = false;         
        } 

How to split form background color in C Sharp

//spliting form background color on Split button click
        private void btnSplit_Click(object sender, EventArgs e)
        {           
            fnSplit();
//Calling function to split form color.
            check = true;
//assigning true to check.
        }

How to split form background color in C Sharp 

        private void frmColor_SizeChanged(object sender, EventArgs e)
        {
            if(check)
                fnSplit();
        //calling fnSplit() function is form background color is already split on form resize event.
        }
 
//this is reset the form bolor to its default color.
        private void btnReset_Click(object sender, EventArgs e)
        {          
            this.BackColor = DefaultBackColor;
            if (check)
            {
                this.Refresh();
                this.BackColor = DefaultBackColor;
            }
        }   

How to split form background color in C Sharp



c# c# 
Updated 04-Mar-2020
I am a content writter !

Leave Comment

Comments

Liked By