none
Button.Enabled = False is Not Working RRS feed

  • Question

  • Hello,

    I'm stumped. Help me see if there's something I'm missing.

    My button cmdSave.Enabled = False line of code is not disabling the button on a Windows form that is being loaded by the click of another button. Here's a few things I'm seeing:

    1. To check that the property is set, immediately after setting the cmdSave button's enabled property to false, I assigned the button's enabled property to an object variable which reads as false...as it should. However, this button is still enabled on the form when it loads.

    2. In the form's load event, I added another cmdSave.Enabled = False line of code, which is the only procedure there - but, the button is still Enabled when the form loads.

    3. I shut down Visual Studio, then reopened everything, and tried to run the code as is...The first time I clicked the button that loads the form, the cmdSave button was disabled correctly. Just this once. So, when I closed the form, then opened it again, it was enabled again and every time following that.

    4. The Enabled property for this button in the form designer is also set to False, but it is always enabled when the form loads.

    Is there something else I can do to force this button to be disabled on form load?

    Sunday, June 17, 2018 3:12 AM

Answers

  • Is there something else I can do to force this button to be disabled on form load?

    The form load event is not a good choice for this type of adjustment, because there is no guarantee that all controls have been loaded when the form load event fires.  So a setting from that method might get overridden when the control loads.

    Put the code in the form Shown event - that occurs after everything else on the form has been fully initialised.

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/order-of-events-in-windows-forms

    That doesn't address the issue that setting the value in the designer is not working.

    • Marked as answer by Don Petry Sunday, June 17, 2018 3:38 AM
    Sunday, June 17, 2018 3:29 AM

All replies

  • Is there something else I can do to force this button to be disabled on form load?

    The form load event is not a good choice for this type of adjustment, because there is no guarantee that all controls have been loaded when the form load event fires.  So a setting from that method might get overridden when the control loads.

    Put the code in the form Shown event - that occurs after everything else on the form has been fully initialised.

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/order-of-events-in-windows-forms

    That doesn't address the issue that setting the value in the designer is not working.

    • Marked as answer by Don Petry Sunday, June 17, 2018 3:38 AM
    Sunday, June 17, 2018 3:29 AM
  • Thank you, sir.

    My brain was hurting, so I restarted the computer and went to take a shower. Then, I thought about putting the code in the shown event as well at that time. That worked, but it still hurts to think that there was no reason it shouldn't have worked outside of that.

    Sunday, June 17, 2018 3:43 AM