0

The following code reads perfectly the values I am looking to fill in textboxes and compare for other calculations. But, at the same time, one of them, the getColorFromTable = dataReader["ReqCol"].ToString(); returns the colorof the device being calculated. I am trying to find a way that the application reads the same color from the table and assign it to the following: txPaintColorUpdate.ForeColor and txPaintColorUpdate.BorderColor

So, there are four colors in the table: Red, Orange, Green and Light Blue. After the calculation occurs, the system assigns the type of color to the device, but as an indicator, should change the color of txPaintColorUpdate as mentioned above.

Here is a portion of code:

                conn.Open();

            SqlDataReader dataReader;
            dataReader = cmd.ExecuteReader();

            if (dataReader.Read())
            {
                minimumFlowCapacity = dataReader["MinFC"].ToString();
                maximumFlowCapacity = dataReader["MaxFC"].ToString();
                getColorFromTable = dataReader["ReqCol"].ToString();
                getClassification = dataReader["Class"].ToString();
            }

Thus, variable getColorFromTable is receiving the name of the color from the table and displaying it in the TextBox (txPaintColorUpdate). Any advice how to read the same color name so C# can assign it to the TextBox? I know that the common way to do that is invoking txPaintColorUpdate.BorderColor = System.Drawing.Color.Red; and txPaintColorUpdate.ForeColor = System.Drawing.Color.Orange;, but maybe there is an easier way without doing a lot of if() else{} statements.

This is a portion of code of when the calculation is made and should assign the color to the TextBox:

               if (flow20PSI >= Convert.ToDouble(minimumFlowCapacity) && flow20PSI <= Convert.ToDouble(maximumFlowCapacity))
            {
                txPaintColorUpdate.Text = getColorFromTable;

                //txPaintColorUpdate.ForeColor = System.Drawing.Color(txPaintColorUpdate.);
                //txPaintColorUpdate.BorderColor = System.Drawing.Color.Red;
            }
Mr. Munoz
  • 69
  • 1
  • 3
  • 13

1 Answers1

0

Thanks @Etienne, your solution led me to find the one I needed using this:

public static Color FromName(string colorName)

{
    System.Drawing.Color systemColor = System.Drawing.Color.FromName(colorName);   
    return new Color(systemColor.R, systemColor.G, systemColor.B, systemColor.A); //Here Color is Microsoft.Xna.Framework.Graphics.Color
}

From: Convert string to Color in C#

My solution was only using this: System.Drawing.Color systemColor = System.Drawing.Color.FromName(getColorFromTable);

Community
  • 1
  • 1
Mr. Munoz
  • 69
  • 1
  • 3
  • 13