Click here to Skip to main content
15,896,544 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am a newbie to c# programming and creating a simple rolldice game in windows forms. My form consists of two pictureboxes, mysumLabel label, rolldice button and Tablelayoutpanel consisting of 9 buttons labelled 1-9. The game simulates the rolling of two six sided dice. When clicking the rolldice button the dice face images get randomly displayed in the two pictureboxes and the sum of their number of faces get displayed on the mysumLabel label. Based on the value of mysum label, the player has to color the corresponding button in the Tablelayoutpanel by clicking on it. The player wins if he gets three buttons colored in a row, column or diagonally. The functionality for winning is working fine my problem is that I only want the button corresponding to the value of mysumLabel label to be enabled, the rest of buttons in the Tablelayoutpanel should be disabled. Something like pseudocode below:

C#
If(mysumLabel.Text= myButton1.Text)
{
  myButton2.Enabled= myButton3.Enabled= myButton3.Enabled= myButton4.Enabled=
  myButton5.Enabled= myButton6.Enabled= myButton7.Enabled= myButton8.Enabled=
  myButton9.Enabled=false
}
else
{

}
Posted
Updated 6-Aug-14 3:06am
v3
Comments
Leo Chapiro 6-Aug-14 9:15am    
Sorry, I have not understand your concept: why do you have 9 Buttons for 2 dices (not 6*2 = 12 e.g.)?

1 solution

Hi,dihawa you can add your buttons to an Button array and then you can enable and disable buttons as you want.Here is a sample code matches your requirement. Hope this helps to you :-)

C#
Button[] arrbtn = new Button[4];
arrbtn[0] = myButton1;
arrbtn[1] = myButton2;
arrbtn[2] = myButton3;
arrbtn[3] = myButton4;

  foreach(Button k in arrbtn)
     {
       if (mysumLabel.Text.Equals(k.Name))
           {
              k.Enabled = true;
           }
       else
           {
              k.Enabled = false;
           }
     }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900