Answer to Question #331983 in C# for Kann

Question #331983

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.

1
Expert's answer
2022-04-21T14:37:55-0400
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();
            }
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS