Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to change color of panel with generated color from red, green and blue button click ?

i have three button and one panel. When i press button red and green its output color will show in panel and so on..........
Posted

If you want to change color with clicks on buttons then
connect these event handlers to each button's Clicked event.
C#
private void btnRed_Clicked(object sender, EventArgs e)
{
   panel.BackColor = Colors.Red;
}

private void btnGreen_Clicked(object sender, EventArgs e)
{
   panel.BackColor = Colors.Green;
}

private void btnBlue_Clicked(object sender, EventArgs e)
{
   panel.BackColor = Colors.Blue;
}


In order to change color with combination of buttons I suggest to replace buttons with checkBoxes and add this event handler to each checkBox's Checked event.

C#
void checkBox_Checked(object sender, EventArgs e)
{
  int state = 0;
  if(btnRed.Checked)
  {
   //001
    state += 1;
  }

  if(btnGreen.Checked)
  {
    //010
    state += 2;
  }

  if(btnBlue.Checked)
  {
    //100
    state += 4;
  }
 
  switch(state)
  {
    //red
    case 1:
       panel.BackColor = Colors.Red;
       break;
    //green
    case 2:
       panel.BackColor = Colors.Green;
       break;
    //blue
    case 4:
       panel.BackColor = Colors.Red;
       break;
    //red + green
    case 3:
        //whatever you think is appropriate 
        break;
    //
    //code for other combinations goes here
    //

    default:
        panel.BackColor = SystemColors.Control;
  }
}
 
Share this answer
 
All you'd need to do would be to set the BackColor.
C#
private void button2_Click(object sender, EventArgs e)
{
    panel1.BackColor = Color.Yellow;
}

private void button3_Click(object sender, EventArgs e)
{
    panel1.BackColor = Color.Red;
}
 
Share this answer
 
v2
Set the BackGroudColor property of the Panel on the button click.
If Red Button is clicked set the color as Red and so on.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900