Wednesday, March 16, 2011

Making passcode application in C#

This is a tutorial making a simple passcode application in C#
Video tutorial is included :)

  • Visual Studio with C#
  • Basic knowledge of C# and Programming

Quick step-by-step:

Step 1: Create a new project
  • Start Microsoft Visual Studio 2010
  • In Menu, click File - New - Project
  • Select Visual C#
  • Select Windows Form Applications
  • Name your project, eg. Passcode
  • Click OK

Step 2: Designing your form
  • Begin designing your form by opening the Toolbox
  • Your passcode application will have a textbox, numeric button, clear button and enter button.
  • Textbox will display entered passcode, numeric button (0,1,2,3 - 9) is for the user to click, clear button is to clear the entered passcode and enter button to check the passcode entered.
  • Double-Click or Drag Drop the textbox from toolbox to the form
    • Resize the textbox accordingly
    • You may want to change the text style and size for this textbox. Right-click on the textbox and click Properties. Change the Font value.
    • Rename this textbox, eg. txtPass. Right-click the textbox and change the value of (Name)
  • Now, add a button
    • Resize and change font styles.
    • Change the button text, on Properties, change the value of text to "1" (without quotes)
    • Optional: Rename the button, eg. btn1
    • Repeat the same step for other numeric button, Clear button and Enter button
  • Resize and decorate your form

Step 3: Code your application
  • Code the numeric button event, double-click the first button which is 1
    • Automatic code will be created for the button click event
    • Add this code:
      • txtPass.Text = txtPass.Text + "1";
    • txtPass refer to the textbox you named earlier.
    • the code simply append a number 1 (string) to the textbox without deleting previous text
    • Repeat the same code for other numeric button
  • Code the Clear button, double-click the Clear button.
    • Automatic code will be created
    • Add this code:
      • txtPass.Text = "";
    • This code will simply replace the textbox with an empty string
  • Code the Enter button, double-click the Enter button
    • Add this code:
      • if (txtPass.Text == "12345")
        txtPass.Text = "Passed";
        txtPass.Text = "Wrong Passcode! Please try again";

    • This code compares the text entered in txtPass with the string value 12345
    • If txtPass equals to 12345, it then replaced the text in txtPass to "Passed". You can change eg. letting the user into the application after passcode entered correctly
    • Vice-versa for entering wrong passcode combination

 Step 4: Test your passcode application
  • To run your application, click on Debug - Start without debugging
  • The application will start and try to enter some value
  • Finish! :)

Step 5: Automatic exit after entered incorrectly 3 times
  • Go back to the code.
  • Go to the top, where you can find class text
  • Add this variable code under the class:
    • int nTry = 0;
  • The reason to add the variable here is because the variable value will retain until the application exit
  • Now, go back to the Enter button code:
  • Change to:
      • if (txtPass.Text == "12345")
        txtPass.Text = "Passed";
        txtPass.Text = "Wrong Passcode! Please try again";
        if (nTry == 3)

    • nTry will be incremented each time you entered wrong passcode combination
    • If the value of nTry is equal to 3, then the Application.Exit() will be executed.
  • Done! Test again with wrong passcode. Enter it three times and see your application will exit itself.

See the video tutorial here:

Note: Since this is a simple passcode application, the security of the application is too weak and some people will find out the real passcode easily.