Reputation: 57488
When manipulating controls on a .NET windows form which of the following is best practice and why?
//Hide control from user and stop control form being useable
oControl.Enabled = false;
oControl.Visible = false;
or
//Hide control from user and stop control form being useable
oControl.Visible = false;
I've been using the first case and always disabling a control when hiding it, but I've been told that this is wrong and that I should only be hiding it. I seem to vaguely remember reading somewhere that if you don't specifically dissable a control that it can continue to interact with the user.
Any enlightenment would be apreciated.
Upvotes: 11
Views: 8415
Reputation: 86729
Enabled
refers to whether or not the user can interact with the control (i.e. if the control is grayed out or not)
Visible
refers to wehether or not the control is displayed (usually if this is false the control is not rendered at all, however not all the time apparently - see the comments of this post).
If the control is not rendered then the value of the enabled propery will have no impact.
Upvotes: 9
Reputation: 158289
Whether you will need to set Enabled = false
when hiding a control depends on the control in question, and what kind of interaction it offers. For many controls (such as a Button
or a CheckBox
), setting Visible = false
will suffice to prevent any interaction between the user and the control.
But some controls (it seems to be especially those offering a Shortcut key property), will still offer user interaction when not visible. For instance the ToolStripMenuItem
(and the "older" MenuItem
) will still have their Click
event invoked when the shortcut key is pressed, regardless of Visible
being true
or false
.
Setting Enabled = false
will prevent invoking the Click
event through shortcut keys in those cases. From that point of view, I would not advice against setting Enabled = false
when hiding a control in a WinForms application.
Upvotes: 10
Reputation: 76057
If you are asking also about usability rather than just technical matters, I won't recommend you to hide things (unless you're changing completely the current "view" of your application), because usually it's less annoying find a control disabled (it gives you a feedback about the action you want to do is not ready yet) than spend a few seconds searching it, just to realize after a while that it must be disabled because the preconditions to use it are not satisfied.
If you were already aware about this, just ignore it :-p
Upvotes: 3
Reputation: 137118
From the MSDN:
Elements where Visibility is not Visible do not participate in input events (or commands), do not influence either the Measure or Arrange passes of layout, are not in a tab sequence, and will not be reported in hit testing.
So I think you can assume that setting .Enabled = false
is unnecessary.
UPDATE
I've checked the .Visibity
documentation on the MSDN, but unfortunately it doesn't say anything about whether the control is disabled or not.
Upvotes: 8
Reputation: 5862
Not sure about .NET, but actionscript/Flex has three distinct properties for controls that take boolean values.
enabled
visible
includeInLayout
Setting the visible property false keeps it around and can effect layout. It is still drawn by the display renderer. Setting includeInLayout property keeps it from being rendered all together. Often I find it useful to include all the properties depending what I want to happen with the control and my view. There may be a similar property in .NET. But I am not sure.
Upvotes: 1
Reputation: 20271
For basic controls like labels or text boxes, I don't think it makes any actual difference which method you use.
But consider a more complex control, that contains a timer to check if there is new data to display; disabling the control also disables the timer.
If you make it invisible without disabling it, the timer still fires events, and any new data still gets processed. If you disable it too, new data does not get processed. It depends on the specific case, which of the two behaviours you want.
FWIW, I disagree with the person who told you that it was wrong to hide and disable. I think that it's unnecessary, in most cases.
Upvotes: 3
Reputation: 11438
A quick test shows that setting Visible to false also disables the accelerator keys for that control.
Under Win32 (ie. this doesn't apply to Windows Forms), accelerators remain enabled when the control is hidden but not disabled. I assume that this is the reference you were thinking of.
Upvotes: 5
Reputation: 25775
Unless this is some special control that can receive focus even when invisible, I don't think you need to disable it explicitly. Simply turning off the visibility should be sufficient to prevent the user from interacting with the control.
I wouldn't say it is "Wrong", however. I'd describe it as "overkill".
Upvotes: 2