views:

628

answers:

3

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?

+3  A: 

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).

Michael Todd
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
@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.
OregonGhost
+1  A: 

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.

Joel Coehoorn
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
+1  A: 

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

James