Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling the hover behavior on winform buttons

Tags:

c#

winforms

I'm developing a winform application using C# 4.0

I have a form with one button. I changed the button's BackColor to yellow. At runtime, the button's back color changes slightly when I move the mouse over it. I want to disable this. I want the color to remain the same no matter what happens.

Here's the form code:

using System;
using System.Windows.Forms;

namespace Something
{
    public partial class Home : Form
    {
        public Home()
        {
            InitializeComponent();
        }

    }
}



namespace Something
{
partial class Home
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Home));
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // button1
        // 
        this.button1.BackColor = System.Drawing.Color.Yellow;
        resources.ApplyResources(this.button1, "button1");
        this.button1.Name = "button1";
        this.button1.UseVisualStyleBackColor = false;
        // 
        // Home
        // 
        resources.ApplyResources(this, "$this");
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(226)))), ((int)(((byte)(227)))), ((int)(((byte)(228)))));
        this.Controls.Add(this.button1);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Name = "Home";
        this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
        this.Load += new System.EventHandler(this.Home_Load);
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.Button button1;



}
}

Thanks in advance.

like image 530
user2931442 Avatar asked Oct 29 '13 09:10

user2931442


3 Answers

If you already set FlatStyle to flat, it's simple that you can do something like this:

//place this code in your form constructor
button1.FlatAppearance.MouseOverBackColor = button1.BackColor;
button1.BackColorChanged += (s, e) => {
   button1.FlatAppearance.MouseOverBackColor = button1.BackColor;
};
like image 107
King King Avatar answered Nov 02 '22 14:11

King King


I think you have already set FlatStyle to Flat. In flatAppearance, we can change MouseOverBackColor into Transparent.

like image 7
E J Chathuranga Avatar answered Nov 02 '22 12:11

E J Chathuranga


People like me don't know what a compiler is. I looked up what it was and found out how to do it. It should look something like this.

public Menu2**()
    {
        button1.FlatAppearance.MouseOverBackColor = button1.BackColor;
        button1.BackColorChanged += (s, e) => {
            button1.FlatAppearance.MouseOverBackColor = button1.BackColor;
        };

**Menu2 is the name of the form you're working on.

like image 2
DutchJelly Avatar answered Nov 02 '22 13:11

DutchJelly