Form tool class (5) Mouse access to change the background color of the control

In development, there are some commonly used methods. It is troublesome to find the previously written ones every time. If you write your own, you are doing repetitive work, so there is always a small tool class and you can add content at any time.

5. winform access control to change the background color static class  FormTools (public static class FormTools)

5.1 Control A triggers to change its own background color (here A: control)

            /// <summary>
            /// The mouse moves into the background animation (registered control A only triggers events and only changes the background of A)
            /// </summary>
            /// <param name="contrl">The control to trigger the event</param>
            /// <param name="hoverColor">Background color when mouse is accessed</param>
            public static void HoverBackColor(this Control contrl, Color hoverColor)
            {
                var leaveColor = contrl.BackColor;
                contrl.MouseEnter+= (sender, e) =>
                {
                    contrl.BackColor =hoverColor;
                };
                 contrl.MouseLeave+= (sender, e) =>
                {
                    contrl.BackColor = leaveColor;
                };
            }

5.2 Control A triggers to change the background color of control B (here A: contrl, B: con)

            /// <summary>
            /// The mouse moves into the background animation (registered control A triggers an event to change the background color of B)
            /// </summary>
            /// <param name="contrl">The control to trigger the event</param>
            /// <param name="con">Background color to change</param>
            /// <param name="hoverColor">Background color when mouse is accessed</param>
            public static void HoverBackColor(this Control contrl,Control  con, Color hoverColor)
            {
                var leaveColor = con.BackColor;
                contrl.MouseEnter += (sender, e) =>
                {
                    con.BackColor = hoverColor;
                };
                contrl.MouseLeave += (sender, e) =>
                {
                    con.BackColor = leaveColor;
                };
            }
5.3 A group of controls triggers to change the background color of itself (a group of controls: control)
        /// <summary>
        /// The mouse moves into the background animation (registering a group of control controls B, C, D... triggers to change the background color of itself)
        /// </summary>
        /// <param name="contrl">The control to trigger the event</param>
        /// <param name="hoverColor">Background color when mouse is accessed</param>
        public static void HoverBackColor( Control[] contrl,Color hoverColor)
        {
            foreach (var con in contrl)
            {
                var leaveColor = con.BackColor;
                con.MouseEnter += (sender, e) =>
                {
                    con.BackColor = hoverColor;
                };
                con.MouseLeave += (sender, e) =>
                {
                    con.BackColor = leaveColor;
                };
            }

        }
5.4 A group of controls triggers to change the background color of control A (a group of controls: control , control A: con)
            /// <summary>
            /// The mouse moves into the background animation (register a group of controls B, C, D... trigger to change the background color of A)
            /// </summary>
            /// <param name="contrl">The control to trigger the event</param>
            /// <param name="con">Control color to be changed</param>
            /// <param name="hoverColor">Background color when mouse is accessed</param>
            public static void HoverBackColor(this Control con,Control [] contrl, Color hoverColor)
            {
                foreach (var c in contrl)
                {
                    var leaveColor = con.BackColor;
                    c.MouseEnter += (sender, e) =>
                    {
                        con.BackColor = hoverColor;
                    };
                    c.MouseLeave += (sender, e) =>
                    {
                        con.BackColor = leaveColor;
                    };
                }
           
        }

transfer

5.1 Change the background color of the form to red
this.HoverBackColor(Color.Red);
5.2 Change the background color of the form to red when the mouse accesses Panel1 
this.Panel1.HoverBackColor(this, Color.Red);
5.3 When the mouse accesses the control times, label1 changes its own background color to red
FormTools.HoverBackColor(new Control[] { this.times,  this.label1 }, Color.Red);
5.4 Change the background color of panel1 to red when the mouse accesses the control times, label1
this.Panel1.HoverBackColor(new Control[] { this.times,  this.label1},Color.Red);            








Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324407285&siteId=291194637