Reputation: 15
First I created this array of LightGray block, and the button START. So the purpose is when I click START the chosen block will turn ForrestGreen:
Each of the LightGray block stored in an Label array called blockLabel. This blockLabel is added by the panel.
here's the code to handle the button:
private void btnStart_Click(object sender,EventArgs e) {
CreateBlock();
}
private void CreateBlock() {
blockLabel[5,0].BackColor = Color.ForestGreen;
}
blockLabel code:
blockLabel = new System.Windows.Forms.Label[numCol,numRow];
// Create many block label
int i,j;
for (i = 0; i < numCol; i++) {
for (j = 0; j < numRow; j++) {
blockLabel[i,j] = new System.Windows.Forms.Label {
Location = new System.Drawing.Point(33*i,33*j),
Size = new System.Drawing.Size(30,30),
BackColor = Color.LightGray,
TabIndex = numCol*i + j
};
}
}
// World is the panel
for (i = 0; i < numCol; i++) {
for (j = 0; j < numRow; j++) {
World.Controls.Add(blockLabel[i,j]);
}
}
The World panel I add by the Designer
When I click Start nothing seems to happen. Any help pls?
Upvotes: 0
Views: 66
Reputation: 37020
The problem is that your button control is not hooked up to the click event you have defined. You can do this through the designer by selecting the button, then in the Properties
window, click the Event
icon (lightning bolt), then scroll down to the Click
event, and choose your method from the drop down list:
If you're adding the button programmatically, then you can simply add your event hander to the click event like so:
Button btnStart = new Button();
btnStart.Click += btnStart_Click; // This adds your event hander code to the click event
Controls.Add(btnStart);
Upvotes: 2