3

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!

4
  • 2
    Your code should already work, assuming that mybtn.Enabled = false; is executing. It would be clearer if you surrounded that line of code with braces. Commented Nov 7, 2012 at 19:08
  • Small, Complete, Example please Commented Nov 7, 2012 at 19:11
  • dont get discouraged by negative votes. could u elaborate on the code part?? Commented Nov 7, 2012 at 19:12
  • I voted to re-open because it appears as though the author has an actual problem, and could make an answerable question if he edits Commented Nov 7, 2012 at 19:15

2 Answers 2

3

Here's some code that works for a specific example.

I don't see any specific problems with your code, so if I were you, I'd put some breakpoints in to make sure that your condition is fulfilled, and that everything is proceeding as you think it is.

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            if (numericUpDown1.Value > 5)
                button1.Enabled = false;
        }
    }
}

As an additional note, Visual Studio will automatically generate the numericUpDown1_ValueChanged handler when you double-click on the UpDown control in the designer

Sign up to request clarification or add additional context in comments.

2 Comments

Lol, found the answer and went to respond but got network error and yours was already here.. i agree with this one :)
Thanks for that, I'm still pretty new to programming and it was a rather naive mistake for me to make. The code was just being handled by the wrong event; it was being executed after the actual button Click and not the ValueChanged.
1

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;

1 Comment

I just noticed that myself from looking at the previous example but thanks for replying at least now i know what's wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.