C# Tutorial – Make a Number Guessing Game using Windows Form in Visual Studio
- Subject: C# Tutorials
- Learning Time: 30 minutes
Learn how to make a number guessing game in Windows Forms Application in Visual Studio using C# programming language. This is a very simple app aimed for the beginners who want to get up to speed with a small app. This program will generate a random number in the background between 1 and 10. The user will need to guess which number the computer has stored. If your guess is higher than the number then it will show a message on the screen it will do the same if the number is lower. The app will also keep track of how many times you guessed and it will reset when the new number is generated again.
Video Tutorial –
Full Source Code –
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Number_Guessing_Game { public partial class Form1 : Form { Random randomNumber = new Random(); int number = 0; int guesses = 0; public Form1() { InitializeComponent(); loadQuestions(); } private void CheckNumber(object sender, EventArgs e) { int i = Convert.ToInt32(txtEnterNumber.Text); guesses += 1; lblGuessed.Text = "You guessed " + guesses + " times"; if (i == number) { MessageBox.Show("Nice, you guessed it. Try another"); loadQuestions(); txtEnterNumber.Text = ""; guesses = 0; lblGuessed.Text = "You guessed " + guesses + " times"; } else if (i < number) { MessageBox.Show("Go Higher"); } else { MessageBox.Show("Go Lower"); } } private void loadQuestions() { number = randomNumber.Next(0, 10); lblQuestion.Text = "I am thinking of a number between: 0 and 10 "; } } }