7

Simple question: I have a WinForms button, and I want to make it both (conditionally) invisible and disabled (to be sure that if someone clicks in the space where the invisible button lives, it won't activate it.) Does button.Visible = false also imply button.Enabled = false, or do I need to set/reset both properties at the appropriate time?

2
  • 1
    I know, there's no stupid/lazy question, but seriously, why didn't you just tried ? :/ Sep 15, 2009 at 15:19
  • 2
    Why do you need the button disabled if you hide it anyway? Just disable the underlying functionality (by testing some flag, or however you know that you should disable the button). This way, even if some stupid user uses a tool to re-show your button or to send some window message to the invisible, yet enabled button, your application won't do things it shouldn't be doing. In other words, if you want to disable functionality, disable the functionality and not just the front door to it. Sep 15, 2009 at 15:27

4 Answers 4

7

If the control is not visible, it is effectively disabled. Clicking in the area where it would appear (or rolling in and out of that area) were it visible will not cause an event to fire.

EDIT: To clarify, based on other responses and comments, the button is not disabled and underlying event functionality is still available programmatically, but the button will not be physically available/visible on the form and the user will not be able to interact with it in any way (unless you, as the programmer, provide another method programmatically).

2
  • There are ways other than mouse clicking (such as keyboard) to activate a button. I read his question to be asking whether there was any way to depress a button, not just is it accessible via mouse.
    – recursive
    Sep 15, 2009 at 15:25
  • @recursive: While your point is valid, I'd expect a control to lose focus when it is hidden, and that it can't gain focus again. This might not be true though, and I'm not sure what happens to mnemonics. But even then, there are ways to press a button without mouse or keyboard. UI Automation comes to mind. Sep 15, 2009 at 15:50
2

Setting Visible to false does not change the Enabled property. However, setting the property to false does make the control effectively not even there. If you click in the empty space left by an invisible the button, the button's click event won't fire.

1
  • Thanks,Joel. That's basically what I needed. I just need to make sure the user won't be able to click on it.
    – Dave Hanna
    Sep 15, 2009 at 17:03
1

I don't think it implies it is disabled. It just means the control is not visible on the form hence there is no way to perform the action on it. If you set the visible property to false and then invoked the Click event through code it would process. However, if you set the Enabled property to False I would imagine it wouldn't

1

Pretty sure if .Visible = false, the '_Click' action is disabled. For instance, if you .PerformClick() in your code, and .Visible = true, _Click will execute. If false, _Click will not execute.

1
  • According to the other answers, this is incorrect.
    – DOSLuke
    Oct 8, 2023 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.