Siegeon
Siegeon

Reputation: 610

Windows forms button.Enabled not working visually

I am creating a windows forms button dynamically wrapped in another control. The issue that I am having is that when I set the buttons Enabled property it is still being displayed as though it were enabled (not greyed out) however it is not clickalbe. This leads me to believe that I am not creating the button correctly, or something else like that.

This is the code that I use to create the button.

private System.Windows.Forms.Button CreateWindowsButton(SessionButtonTypes sessionButtonType)
{
        windowsButton = new System.Windows.Forms.Button()
        {
            Top = 3,
            Name = sessionButtonType.ToString(),
            Width = DeterminButtonWidth(guiElement),
            Height = 45,
            FlatStyle = FlatStyle.Flat,
            BackgroundImage = GUI.Instance.GUIImageElement(guiElement)
        };
        // set windows button flat border parameters
        windowsButton.FlatAppearance.BorderSize = 0;

        // for testing
        windowsButton.Enabled = false;
}

[UPDATE] The solution turns out to be putting image element into the image parameter, not the background image parameter.

Upvotes: 3

Views: 3002

Answers (2)

bdrudolph
bdrudolph

Reputation: 179

I had the same problem and for me the solution was changing flatstyle from "Standard" to "System"

Upvotes: 0

Saintt Sheldon Patnett
Saintt Sheldon Patnett

Reputation: 1163

Remove `FlatStyle = FlatStyle.Flat`.

Using FlatStyle forces the control to have a predefined style which would make the button appear not greyed out even when Enabled is equal to false.

Upvotes: 2

Related Questions