In this article, we’ll learn How to create dice roll program in C# Windows Form.
Step 1:
Create a windows application and design the form as follows.
Open Visual Studio ->File -> New Project ->Visual C#-> select Windows Forms Application
Give the name of the application and click on OK.
Step 2:
Form Design
Step 3: ImageList provides a container for image data. The control is not visible directly. It is instead referenced from other controls such as ListView, which acquire the images from index values into the ImageList.
Step 4:
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { timer1.Start(); button1.Enabled = false; } int counter = 0; private void timer1_Tick(object sender, EventArgs e) { int dice1, dice2; Random rnd = new Random(); dice1 = rnd.Next(0, 6); dice2 = rnd.Next(0, 6); pictureBox1.Image = ımageList1.Images[dice1]; pictureBox2.Image = ımageList1.Images[dice2]; counter++; if (counter == 30) { timer1.Stop(); counter = 0; button1.Enabled = true; } } } |