在用户选择上更改标签的边框颜色

时间:2014-02-10 10:00:03

标签: c# event-handling

我想在用户选择他想要的颜色时更改标签的边框颜色。我知道标签的绘画事件可以改变边框颜色。我只想要这段代码:

private void label1_Paint(object sender, PaintEventArgs e)
{
    ControlPaint.DrawBorder(e.Graphics, label1.DisplayRectangle, Color.Blue, ButtonBorderStyle.Solid);
}

继续工作:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
      if (comboBox1.SelectedIndex == 0)
      {
           //apply red color
      }
}

要应用的组合框的选定索引(用户想要的颜色)。我只是不知道如何。

1 个答案:

答案 0 :(得分:0)

Color selectedColor;

private void label1_Paint(object sender, PaintEventArgs e)
{
    base.OnPaint(e);
    ControlPaint.DrawBorder(e.Graphics, label1.DisplayRectangle, selectedColor, ButtonBorderStyle.Solid);
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox1.SelectedIndex == 0)
    {
        selectedColor = Color.Red;         
    }
    if (comboBox1.SelectedIndex == 1)
    {
        selectColor = Color.Blue;
    }
    label1.Invalidate();
    label1.Update();
}