Loading...

Change the Color and Border of Buttons using C#

Changing Color and border of button in Windows Form Application

If you want to change the colors and the border style of a button then this post will sincerely help you a lot.About two years ago I was making my semester project and I wanted to change the color and border style of button on Windows Form Application to make my project more attractive. I searched  few hours and get the idea, Now I am giving you the complete Idea here. Firstly you have to know that what are Windows Controls events. 

Windows Form Application Controls events:




  • Windows form application control are both built-in and custom. Built-in controls are the most commonly used controls such as button, text box, combo box, etc. Each control has some events, these event are same as functions we use in Object Oriented Programming Languages.
  •  If an specific event is made then the event will run its whole code whenever the specified work is done on the application. For example if we make a click event of a button then the whole code in the event code will be executed whenever the button is clicked. 
  • We will use mouse_enter event of a button, means that the event code will run whenever the mouse pointer enter that button. we will change the button of color when mouse enter and again do the same when mouse leaves by using mouse_leave event.
  • You can select the event by double clicking in the intended events by selecting events from the very same panel where we change the properties of a control. The below picture will help to find out.

Double click the highlighted field near mouse enter and mouse leave to make two events in code. the two below events will be made. rgister_blue is the name of button.


       private void rgister_blue_MouseEnter(object sender, EventArgs e)
        {
            // this code will run whenever the mouse pointer enters the button
        }

       private void rgister_blue_MouseLeave(object sender, EventArgs e)
        {
            // this code will run whenever the mouse pointer leaves the button
        }  

Code to change the Color and Border:


rgister_blue is the name of button.


            rgister_blue.FlatStyle = FlatStyle.Flat; // to change to the flat style for borders

            rgister_blue.FlatAppearance.BorderColor = Color.Black; // change border color

            rgister_blue.BackColor = Color.LightSkyBlue; // change button background color

            rgister_blue.FlatAppearance.BorderSize = 5; // border size


To revert it back:


      rgister_blue.FlatStyle = FlatStyle.Standard;


            rgister_blue.FlatAppearance.BorderColor = Color.White;
            rgister_blue.BackColor = Color.White;
            rgister_blue.FlatAppearance.BorderSize = 0;

You have to write the above code in event to execute so the event will look like this

    private void rgister_blue_MouseEnter(object sender, EventArgs e)
        {
            rgister_blue.FlatStyle = FlatStyle.Flat; // to change to the flat style for borders
            rgister_blue.FlatAppearance.BorderColor = Color.Black; // change border color
            rgister_blue.BackColor = Color.LightSkyBlue; // change button background color
            rgister_blue.FlatAppearance.BorderSize = 5; // border size

        }

       private void rgister_blue_MouseLeave(object sender, EventArgs e)
        {
            rgister_blue.FlatStyle = FlatStyle.Standard;
            rgister_blue.FlatAppearance.BorderColor = Color.White;
            rgister_blue.BackColor = Color.White;
            rgister_blue.FlatAppearance.BorderSize = 0;

        }  


Learn how to check direction of mouse in windows form application here

Hit Like Button if you understood, It helps a lot to keep us Motivated.

0 comments:

Post a Comment

Google+

 
TOP