Skip to main content
deleted 152 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Trying to find a better solution than using a long if, if else, etc Changing button colors based on user input

I am using C# to get the usersuser's input of how many buttons to change the color of. The buttons are in rows that have 1-10 button in each. If the user enters 11 than, then the entire first row will be filled out and the first button on the second row will change color. I have figured out how to accommodate for different numbers the user might enter, but hoping to get some advice on how to code better with outwithout using a large ifif, if elseif else, statement. Here is my code

    internal void ChangeTileColor(ConnectorData connectorData, int j)
    {
        int row = 0;
        if (j < 10)
        {
            
        }
        else if (j < 20)
        {
            j -= 10;
            row++;
        }
        else if (j < 30)
        {
            j -= 20;
            row += 2;
        }
        else if (j < 40)
        {
            j -= 30;
            row += 3;
        }
        else if (j < 50)
        {
            j -= 40;
            row += 4;
        }
        else if (j < 60)
        {
            j -= 50;
            row += 5;
        }
        Button button = this.tableLayoutPanel1.GetControlFromPosition(j, row) as Button;
        button.BackColor = Color.Lime;
    }

Trying to find a better solution than using a long if, if else, etc

I am using C# to get the users input of how many buttons to change the color of. The buttons are in rows that have 1-10 button in each. If the user enters 11 than the entire first row will be filled out and the first button on the second row will change color. I have figured out how to accommodate for different numbers the user might enter, but hoping to get some advice on how to code better with out using a large if, if else, statement. Here is my code

    internal void ChangeTileColor(ConnectorData connectorData, int j)
    {
        int row = 0;
        if (j < 10)
        {
            
        }
        else if (j < 20)
        {
            j -= 10;
            row++;
        }
        else if (j < 30)
        {
            j -= 20;
            row += 2;
        }
        else if (j < 40)
        {
            j -= 30;
            row += 3;
        }
        else if (j < 50)
        {
            j -= 40;
            row += 4;
        }
        else if (j < 60)
        {
            j -= 50;
            row += 5;
        }
        Button button = this.tableLayoutPanel1.GetControlFromPosition(j, row) as Button;
        button.BackColor = Color.Lime;
    }

Changing button colors based on user input

I am using C# to get the user's input of how many buttons to change the color of. The buttons are in rows that have 1-10 button in each. If the user enters 11, then the entire first row will be filled out and the first button on the second row will change color. I have figured out how to accommodate for different numbers the user might enter, but hoping to get some advice on how to code better without using a large if, if else, statement.

internal void ChangeTileColor(ConnectorData connectorData, int j)
{
    int row = 0;
    if (j < 10)
    {
        
    }
    else if (j < 20)
    {
        j -= 10;
        row++;
    }
    else if (j < 30)
    {
        j -= 20;
        row += 2;
    }
    else if (j < 40)
    {
        j -= 30;
        row += 3;
    }
    else if (j < 50)
    {
        j -= 40;
        row += 4;
    }
    else if (j < 60)
    {
        j -= 50;
        row += 5;
    }
    Button button = this.tableLayoutPanel1.GetControlFromPosition(j, row) as Button;
    button.BackColor = Color.Lime;
}
Source Link
Aaron
  • 955
  • 3
  • 11
  • 17

Trying to find a better solution than using a long if, if else, etc

I am using C# to get the users input of how many buttons to change the color of. The buttons are in rows that have 1-10 button in each. If the user enters 11 than the entire first row will be filled out and the first button on the second row will change color. I have figured out how to accommodate for different numbers the user might enter, but hoping to get some advice on how to code better with out using a large if, if else, statement. Here is my code

    internal void ChangeTileColor(ConnectorData connectorData, int j)
    {
        int row = 0;
        if (j < 10)
        {
            
        }
        else if (j < 20)
        {
            j -= 10;
            row++;
        }
        else if (j < 30)
        {
            j -= 20;
            row += 2;
        }
        else if (j < 40)
        {
            j -= 30;
            row += 3;
        }
        else if (j < 50)
        {
            j -= 40;
            row += 4;
        }
        else if (j < 60)
        {
            j -= 50;
            row += 5;
        }
        Button button = this.tableLayoutPanel1.GetControlFromPosition(j, row) as Button;
        button.BackColor = Color.Lime;
    }