更改随机标签的背景颜色

时间:2016-05-04 09:28:30

标签: c# random label tablelayoutpanel

我创建了一个填充TableLayoutPanel的4x4 Labels。我想在BackColor更改一个随机Label的{​​{1}}。我不知道如何做到这一点。 以下是我到目前为止的情况:

Button_Click

2 个答案:

答案 0 :(得分:0)

实际上你并不需要那个阵列。您可以使用TableLayoutPanel的控件集合。 试试这个:

start on filesystem
stop on shutdown

env logf="/home/someuser/miscservices.log"
script
    echo "Script it starting..." >> $logf
    /home/someuser/Downloads/cuckoo-modified-master/miscservices.conf >> $logf
    echo "Script is ended." >> $logf
end scipt

为表lauout面板中的所有标签创建一个公共事件处理程序,并获取所单击控件的索引:

// 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;

答案 1 :(得分:0)

您没有在数组中创建标签实例。您应该首先创建每个标签的新实例:

ZmienKolor

同样在0方法中,您应该在41之间生成随机数,而不是4int i = los.Next(0, 4); int j = los.Next(0, 4);

file_get_contents('php://input')