Search code examples
c#rgbbackcolor

Set BackColor RGB values via trackbar


I'm trying to make an application in c# that lets the user set the backcolor's .R .G .B of the application using 3 Trackbars.

This is the code I came up with to change the value when the trackbar_Scroll event is called.

    private void trb_G_Scroll(object sender, EventArgs e)
    {
        string bgc_G = this.BackColor.G.ToString();
        int bgc_GNewValue = Convert.ToInt32(bgc_G);
        bgc_GNewValue--;

        this.BackColor.G = bgc_GNewValue;
    }

So far everything is working except for 1 thing. When I try to set the value using this.BackColor.G = bgc_GNewValue; it wont work.

It gives me the following error

Property or indexer 'System.Drawing.Color.G' cannot be assigned to -- it is read only.

I've already tried to cast the bgc_GNewValue to a Byte. I dont know what I can do to make this work the way I want it to.

Any help would be very much appreciated.


Solution

  • Fixed my problem. I was overthinking the problem. All I had to do was use this:

    this.BackColor = Color.FromArgb(trb_R.Value, trb_G.Value, trb_B.Value);