Edwardo
Edwardo

Reputation: 902

One method successfully toggles a control's "Enabled" property - a similar method fails. Why?

In my code behind I have two method to enable or disable a group of controls depending upon the value of a field on a form. One of these methods works as expected, and the other doesn't ... quite. The one that works depends upon the value of a Checkbox, while the other depends upon the value of a drop-down list (I believe that this difference is irrelevant - I mention it only for completeness). I've simplified these methods for readability, but the only significant difference from the production system is that they set a number of controls, not just one.

    private void SetControlsFromDropDown(int statusID)
    {
        // This method doesn't work
        bool enable = (statusID == (int)ReqStatus.CompletedOK)
        this.myTextBox.Enabled = enable;
    }


    private void SetControlsFromCheckBox(bool enable)
    { 
        // This method works
        cboMyDropDown.Enabled = enable;
    }

Where the first method fails is that it sets the controls correctly when the form is loaded. However, when the drop-down list changes, the method is called and the value of the bool variable "enable" is correctly set, and the code runs through as expected (and a watch on the Enabled property of the controls that are being set shows that they are toggled as expected) - BUT BUT BUT the controls remain firmly unchanged in the interface. So, if they were initially set to Enabled = false they remain disabled even though the method might have set them to Enabled = true.

What is causing me conniptions is that if I put setting of this.myTextoxBox into the second method, it toggles correctly.

I get the impression I haven't explained myself very clearly. In essence, two more-or-less identical methods, called from similar events, operating in arguably indistinguishable ways, behave differently in real-time. One will toggle the Enabled property of a group of controls ad lib., while the other will toggle it once, never to be toggled again.

Any thoughts gratefully received.

Edward

Upvotes: 0

Views: 70

Answers (2)

Edwardo
Edwardo

Reputation: 902

The problem was caused by a failure of the brain. I was getting an incorrect value from the drop-down list. Apologies for any time wasted.

Upvotes: 1

Cinnamon
Cinnamon

Reputation: 61

I'm not really sure about this, but try to remove the 'this'

Just put the following:

myTextBox.Enabled = enable;

Upvotes: 0

Related Questions