2

I want to program an app where the user can enter a number in a NumericUpDown Control. There are some conditions to the number (e.g. range) that are determined in run time. To give feedback to the user I changed BackColor of NumericUpDown to red when at least one condition is not met. So far it works as i expect it to do.

Now i wanted to add a ToolTip to NumericUpDown to explain why the number was "wrong". When I use the event NumericUpDown_BackColorChanged to adjust the text, the event just won't "trigger". Why is that?

I guess it has to do with the composite character of NumericUpDown as Hans Passant has stated here and here. But I am not sure. TY for the help in advance.

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {
        if (numericUpDown1.Value == 42)
        {
            numericUpDown1.BackColor = Color.Red;
        }
        else
        {
            numericUpDown1.BackColor = System.Drawing.SystemColors.Window;
        }
    }

private void numericUpDown1_BackColorChanged(object sender, EventArgs e)
    {
        //something epic should happen
        //but somehow my program never reaches these lines :(
    }
Community
  • 1
  • 1

2 Answers2

4

I've confirmed that the NumericUpDown.BackColor property setter indeed does not call OnBackColorChanged (in .NET 4.0 at least).

I assume this happens because the BackColor property has been overriden in UpDownBase to also set the BackColor of the composited controls inside NumericUpDown and maybe someone forgot to call the base implementation (I'd love to hear from some of the WinForms gurus here).

What you can do is create a FixedNumericUpDown control that inherits from NumericUpDown and overrides the BackColor property as such:

public Color BackColor
{
    override set
    {
        base.BackColor = value;
        OnBackColorChanged(EventArgs.Empty);
    }
}
Rotem
  • 21,452
  • 6
  • 62
  • 109
0

Suggestion: You can do this by add a ErrorProvider in your by drag and drop from toolbox.

Replace your code as below:

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    { 
        errorProvider1.Clear();

        if (numericUpDown1.Value == 42)
        {
            numericUpDown1.BackColor = Color.Red;
            errorProvider1.SetError(numericUpDown1, "Select another number");
        }
        else
        {
            numericUpDown1.BackColor = System.Drawing.SystemColors.Window;
        }
    }
Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61
  • ty for the input. I will consider this. still the point is that there are some external events that changes the conditions of the number (i.e. that is not the only part in the program that changes the color in red.) Now I am looking for a simple solution that catches every instance where I changed the color in red. – Martin Horatschek Jan 02 '13 at 13:41
  • wherever you are writing numericUpDown1.BackColor = Color.Red; you can also write errorProvider1.SetError(numericUpDown1, "Select another number"); – Mohammad Arshad Alam Jan 02 '13 at 13:45
  • @Arshad While your solution works, it's a workaround, not a solution. The interesting question here is why isn't the event triggering, not what can be done in this particular case to show an error. – Rotem Jan 02 '13 at 13:49
  • @Arshad Obviously I could do that. The point is that i already wrote that part and don't want to go over it all again just to repeat a single line several times (DRY!). – Martin Horatschek Jan 02 '13 at 13:56