Ghost Ghost - 1 year ago 58
C# Question

how to make checkboxes work in accordance with labels c#(yahtzee)

enter image description here
This is my form running, i want to code the checkboxes so that they can hold the picture of their specific dice

Image[] diceImages;
int[] dice;
Random rand;


#region Initialization

public Form1()

private void Form1_Load(object sender, EventArgs e)
diceImages=new Image[7];
diceImages[0] = Properties.Resources.dice_face_0;
diceImages[1] = Properties.Resources.dice_face_1;
diceImages[2] = Properties.Resources.dice_face_2;
diceImages[3] = Properties.Resources.dice_face_3;
diceImages[4] = Properties.Resources.dice_face_4;
diceImages[5] = Properties.Resources.dice_face_5;
diceImages[6] = Properties.Resources.dice_face_6;

dice = new int[5] { 0, 0, 0, 0, 0 };

rand = new Random();


private void btnRoll_Click(object sender, EventArgs e)
for (int i = 0; i < dice.Length; i++)
dice[i] = rand.Next(1, 7);


lblDie1.Image = diceImages[dice[0]];
lblDie2.Image = diceImages[dice[1]];
lblDie3.Image = diceImages[dice[2]];
lblDie4.Image = diceImages[dice[3]];
lblDie5.Image = diceImages[dice[4]];

private void btnExit_Click(object sender, EventArgs e)

private void chk4_CheckedChanged(object sender, EventArgs e)


i have created 5 checkboxes with names chk1-5, i have tried fitting the code in but it doesn't work, could someone help me make this code work.

Answer Source

While your question is pretty unclear as to how you actually want your UI to look like, what might help is the use of UserControls:

You could make a new UserControl named DiceControl that combines one dice image and its corresponding CheckBox. You can then put multiple instances of that control on your form in the designer as if it were just one control. That might help you align stuff the way you want.

All you need to do is create methods/properties on the DiceControl that allow you to get/set the current image and the current hold status.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download