Change panel color

I created some simple code here to just play in C #. It has 3 buttons and 1 panel. If you click on the second and third buttons, the height of the panel will change. Can I also change the color? For example: If I click on the second button, I would like it to be yellow and at the same time change the height. and the same with the third button. Thanks: -)

public partial class Form1 : Form
 {
public int heightPanel;

  public Form1()
    {
  InitializeComponent();
  heightPanel = panel1.Height;
    }

  private void button1_Click(object sender, EventArgs e)
    {
  panel1.Height = heightPanel;
    }

  private void button2_Click(object sender, EventArgs e)
    {
  panel1.Height = this.Height/2;
    }

  private void button3_Click(object sender, EventArgs e)
    {
  panel1.Height = this.Height - 150;
    }
  }

      

I have an idea, but I don't know where to put it. I think it will be something like this: panel1.Height = this.BackColor.ToString (); Any inputs?

thanks

Hi, thanks for the reply. Yes, I would like to keep 3 colors if I click on any of the buttons. I'm not sure if this is possible. eg: my button1 = pink button2 = yellow Button3 = green If I press button 1, I see pink, and if I press button 2, I see pink and yellow.

Is it possible?

Thanks again

0


source to share


3 answers


All you have to do is set BackColor

on a new line. For instance...



private void button2_Click(object sender, EventArgs e)
{
    panel1.Height = this.Height/2;
    panel1.BackColor = Color.Yellow;
}

private void button3_Click(object sender, EventArgs e)
{
    panel1.Height = this.Height - 150;
    panel1.BackColor = Color.Yellow;
}

      

+7


source


Simple as:



panel1.BackColor = Color.Red;

      

0


source


In button2_Click

just add another line:

panel1.BackColor = Color.Yellow;

      

0


source







All Articles