Search code examples
c#winformsbuttonuser-controls

Buttons sometimes doesn't get disabled on windows form


I have a windows form application, and I have UserControl that has some groupboxes and some buttons that should be enabled or disabled based on the user role, like this:

this.btnEditArchive.Enabled = (ActiveUser.Instance.role == DConstants.kAdmin || ActiveUser.Instance.role == DConstants.kSuperAdmin);

This is placed in _Load event. Now sometimes when I switch between pages, these buttons doesn't look disabled. But when I try to touch them, then become disabled. Seems like the UI somehow is not correctly drawn. How to fix this? What may be the cause of this?


Solution

  • It might be wrong, but according to this, _Load event happens only once, when the Form is first called. Maybe putting that code of yours into _Activated event will do the trick?

    EDIT

    I got confused as to what the answer was, but the principle is the same: _Load event only occurs once, when it is called for the first time. So, if you need to have something updated whenever you switch between them, put your code into _VisibleChanged event.