Setting CommandButton's BackColor programmatically.

I am trying to set the BackColor of a CommandButton in the Load event of my Windows Form. However, when I set the BackColor via the code it does not change the BackColor - instead it appears to set the outline of the CommandButton to the Color.

I am using Express Edition. Thank you.



Answer this question

Setting CommandButton's BackColor programmatically.

  • Evangelina

    Hey,

    Yup had come to know bout the Red and Blue switching but just didn't get the time to look into it deeper ...thx anywys..

    Regards,

    Nitin


  • Bill MacKenzie

    errmm..  guess I/we were wrong.   it turns out the code sample shown above doesn't work.  It is because the Red and Blue were switched between RGB and ARGB

    Visual Studio 6.0 RGB numeric format: BBBBBBBBGGGGGGGGRRRRRRRR

    Visual Studio .NET 2005 ARGB numeric format: AAAAAAARRRRRRRRGGGGGGGGBBBBBBBB

    where A=alpha, R=red, G=green, B=blue.

    So you must actually flip around the red and blue components.

    Code to convert old RGB into new ARGB:

    Color clr = Color.FromArgb(Convert.ToInt32(pRGBColor));
    clr = Color.FromArgb(255, (int)clr.B, (int)clr.G, (int)clr.R);

    Code to convert new ARGB back into old RGB

    Color clr = cmdButton.BackColor;
    clr = Color.FromArgb(0, clr.B, clr.G, clr.R);

    Hope this helps someone down the road!

  • douner001

    It appears that the problem is because in VB6 we stored the colors as RGB - and we are working with ARGB in .NET.


  • RodneyJ

    The code is not working because the Alpha Level is not being set. One solution for this is

    button1.BackColor = Color.FromArgb(255,Color.FromArgb(Convert.ToInt32(strColor)));

    Regards,

    Nitin


  • rs2005itw

    strColor = "16776960";
    cmdReadyColor.BackColor = Color.FromArgb(Convert.ToInt32(strColor));

    Is the code I use to set the BackColor. I read the color as a text string (as represented in first line) - then I try to set the back color using the integer value.


  • Srikant0102

    Thanks Nitin - yes, that is a very good way to fix the issue. Better than what I came up with.
  • Setting CommandButton's BackColor programmatically.