Search code examples
c#buttonnumericupdown

How do I disable a button when the value in the NumericUpDown control is greater than a certain number?


The button only disables after I click on it. I want it to disable without any interaction as soon as the value in the NumericUpDown control is incremented above a specific point. I have goggled but found no answers, here is my code:

    private void mybtn_Click(object sender, EventArgs e)
    {            
        if (numericud.Value > myArray[r, c] || myArray[r, c] == 0)
            DisableButton(mybtn);            
        myArray[r, c] = CalcNewMax(myArray[r, c]);
        OpenNewForm();
    }

    private void DisableButton(Button selectedbtn)
    {
        selectedbtn.Enabled = false;
    }

Any help is much appreciated, thanks!


Solution

  • I think you are calling the method "mybtn_Click()" when the button gets clicked, not when the value of the numeric up-down changes. In the "Events" part of the properties of the button check out if the "MouseClick" event is set to call "mybtn_Click()".

    Afterwards go to the "Events" part of the properties of the numeric up-down and set the "ValueChanged" event to call "mybtn_Click()". I also advise you to change the name of the method to a more suitable one before that.

    And lastly, you don't need to make a whole new method for simply changing the Enabled value of the button: simply replace the line

    DisableButton(mybtn);

    with

    mybtn.Enabled = false;