0

I currently code app that at the end should be Football Manager to ProEvolutionSoccer stats converter, but, now, problem is that I am a quiet new in C#, and i have more than 60 numericUpDowns.

Each of theese updowns should change colour at certain vlue, in example, at value 13 backcolour needs to change to green, at 15 to yellow and so on.

Here is example of one I done, on valueChange event:

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
    decimal corners = numericUpDown1.Value;

    if (corners < 13)
    {
        numericUpDown1.BackColor = Color.White;
    }
    else if(corners >= 13 && corners < 15)
    {
        numericUpDown1.BackColor = Color.LightGreen;
    }
    else if (corners >= 15 && corners < 17)
    {
        numericUpDown1.BackColor = Color.Yellow;
    }
    else if (corners >= 17 && corners < 19)
    {
        numericUpDown1.BackColor = Color.Orange;
    }
    else
    {
        numericUpDown1.BackColor = Color.Red;
    }
}

Problem is more than obvious, as I really not feel like writting this block over 60 times and changing just updown indexes or order numbers.

Is there any kind of self refferencing like in php with self keyword, or this or parent, CLASS and so on...

Or some kind of loopping would be better choice?

black_hat_cat
  • 147
  • 1
  • 2
  • 10

3 Answers3

3

You can use the same event handler for each control. Just use the sender parameter instead of the numericUpDown1:

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
    var numericUpDown = (NumericUpDown)sender;
    decimal corners = numericUpDown.Value;

    if (corners < 13)
    {
        numericUpDown.BackColor = Color.White;
    }
...

Then, when subscribing the control, you can use the same handler.

For example, for Asp.Net:

<asp:NumericUpDown id="numericUpDown1" OnValueChanged="numericUpDown_ValueChanged" />
<asp:NumericUpDown id="numericUpDown2" OnValueChanged="numericUpDown_ValueChanged" />
<asp:NumericUpDown id="numericUpDown3" OnValueChanged="numericUpDown_ValueChanged" />

For example, for Winforms:

numericUpDown1.ValueChanged += numericUpDown_ValueChanged;
numericUpDown2.ValueChanged += numericUpDown_ValueChanged;
numericUpDown3.ValueChanged += numericUpDown_ValueChanged;
Ivo
  • 8,172
  • 5
  • 27
  • 42
2

You could have every control reference the same ValueChanged method and then the sender will be which NumericUpDown triggered the event.

Corey Ogburn
  • 24,072
  • 31
  • 113
  • 188
0

Why dont you make a static function/method in a Class like this?

Public class Tools {
    public static Color getColorFromDecimal(decimal corners){
if (corners < 13)
{
    return Color.White;
}
else if(corners >= 13 && corners < 15)
{
    return Color.LightGreen;
}
else if (corners >= 15 && corners < 17)
{
    return Color.Yellow;
}
else if (corners >= 17 && corners < 19)
{
    return Color.Orange;
}
else
{
    return Color.Red;
}
}
}

And then just call it like this

decimal corners = numericUpDown1.Value;
numericUpDown.BackColor = getColorFromDecimal(corners);
Visionstar
  • 355
  • 2
  • 12