更改随机标签的背景颜色
Changing background color of a random Label
我创建了一个 4x4 TableLayoutPanel
填充 Labels
。我想在 Button_Click
处随机更改 Label
中的 BackColor
。我不确定我该怎么做。
这是我目前所拥有的:
public partial class Form1 : Form
{
Label[,] labelki = new Label[4, 4];
Random los = new Random();
Label wylosowanyLabel = null;
public Form1()
{
InitializeComponent();
}
private void ZmienKolor()
{
int i = los.Next(1, 4);
int j = los.Next(1, 4);
wylosowanyLabel = labelki[i, j];
wylosowanyLabel.BackColor = Color.Red; //I get a NullReferenceException here
}
private void btnStart_Click(object sender, EventArgs e)
{
timer1.Start();
ZmienKolor();
}
}
实际上,您不需要那个数组。您可以使用 TableLayoutPanel 的控件集合。
试试这个:
// on the class level:
private int _LabelIndex;
// on the method you want to use for changing the backColor of a lable:
// you might want to change the backcolor of all other lables back to SystemColors.Control.
// if that is the case, you can do something like this:
this.tableLayoutPanel1.Controls.OfType<Label>().ToList().ForEach(c => c.BackColor = SystemColors.Control);
_LabelIndex = los.Next(0, this.tableLayoutPanel1.Controls.Count);
this.tableLayoutPanel1.Controls[labelIndex].BackColor = Color.Red;
为 table lauout 面板内的所有标签创建一个公共事件处理程序并获取被点击控件的索引:
private void labels_click(object sender, EventArgs e)
{
var clickedLabel = (Label)sender;
var index = this.tableLayoutPanel1.Controls.IndexOf(clickedLabel);
if(index == _LabelIndex)
{
// used have clicked the red label.
}
}
您没有在数组中创建标签的实例。您应该首先为每个标签创建一个新实例:
public Form1()
{
InitializeComponent();
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
labelki[i,j] = new Label();
}
}
}
另外,在 ZmienKolor
方法中,您应该在 0
和 4
之间生成随机数,而不是 1
和 4
:
int i = los.Next(0, 4);
int j = los.Next(0, 4);
我创建了一个 4x4 TableLayoutPanel
填充 Labels
。我想在 Button_Click
处随机更改 Label
中的 BackColor
。我不确定我该怎么做。
这是我目前所拥有的:
public partial class Form1 : Form
{
Label[,] labelki = new Label[4, 4];
Random los = new Random();
Label wylosowanyLabel = null;
public Form1()
{
InitializeComponent();
}
private void ZmienKolor()
{
int i = los.Next(1, 4);
int j = los.Next(1, 4);
wylosowanyLabel = labelki[i, j];
wylosowanyLabel.BackColor = Color.Red; //I get a NullReferenceException here
}
private void btnStart_Click(object sender, EventArgs e)
{
timer1.Start();
ZmienKolor();
}
}
实际上,您不需要那个数组。您可以使用 TableLayoutPanel 的控件集合。 试试这个:
// on the class level:
private int _LabelIndex;
// on the method you want to use for changing the backColor of a lable:
// you might want to change the backcolor of all other lables back to SystemColors.Control.
// if that is the case, you can do something like this:
this.tableLayoutPanel1.Controls.OfType<Label>().ToList().ForEach(c => c.BackColor = SystemColors.Control);
_LabelIndex = los.Next(0, this.tableLayoutPanel1.Controls.Count);
this.tableLayoutPanel1.Controls[labelIndex].BackColor = Color.Red;
为 table lauout 面板内的所有标签创建一个公共事件处理程序并获取被点击控件的索引:
private void labels_click(object sender, EventArgs e)
{
var clickedLabel = (Label)sender;
var index = this.tableLayoutPanel1.Controls.IndexOf(clickedLabel);
if(index == _LabelIndex)
{
// used have clicked the red label.
}
}
您没有在数组中创建标签的实例。您应该首先为每个标签创建一个新实例:
public Form1()
{
InitializeComponent();
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
labelki[i,j] = new Label();
}
}
}
另外,在 ZmienKolor
方法中,您应该在 0
和 4
之间生成随机数,而不是 1
和 4
:
int i = los.Next(0, 4);
int j = los.Next(0, 4);