Write a program to act as a digital combination lock safe. Create 3 buttons representing 1, 2 and 3. The user clicks on the buttons trying to guess the correct numbers eg (3321). Use a label to keep track of and display code enrered so far. Only once the correct numbers are pressed, the program congratulates the user with a message.
using System;
using System.Windows.Forms;
namespace lock_safe
{
public partial class MainForm : Form
{
const int correct = 3321;
public MainForm()
{
InitializeComponent();
}
void Button1Click(object sender, EventArgs e)
{
label1.Text = label1.Text + "1";
}
void Button2Click(object sender, EventArgs e)
{
label1.Text = label1.Text + "2";
}
void Button3Click(object sender, EventArgs e)
{
label1.Text = label1.Text + "3";
}
void Button4Click(object sender, EventArgs e)
{
if (Convert.ToInt32(label1.Text) == correct)
{
MessageBox.Show("Congratulations! Code is valid");
Close();
}
else
{
MessageBox.Show("Wrong. Code is invalid");
Close();
}
}
}
}
Comments
Leave a comment