Display a simple password dialog before a program starts in C#

[password dialog]

You might think that a program could start with a password form and then display its main form if the user enters a valid password. Unfortunately when the initial password form closes, it ends the whole application.

A better approach is to make the main form be the startup form. It’s Load event handler displays the password form. If the user successfully enters the password, the program continues as normal. If the user fails to enter the password, the main form closes itself and the program ends.

The following code shows the main form’s Load event handler.

// Display the password form.
private void Form1_Load(object sender, EventArgs e)
    // Display the password form.
    PasswordForm frm = new PasswordForm();
    if (frm.ShowDialog() != DialogResult.OK)
        // The user canceled.

    // Otherwise go on to show this form.

This code creates a PasswordForm and displays it by calling its ShowDialog method. The ShowDialog call returns OK or Cancel depending on whether the user clicked the password form’s OK button or the Cancel button.

The password dialog has a TextBox with PasswordChar set to X so you can’t see what the user is typing. The form’s CancelButton proerty is set to the Cancel button. If the user clicks that button, it automatically sets the form’s DialogResult property to Cancel and hides the form.

If the user clicks the OK button, the following code executes.

// Validate the password.
private void btnOk_Click(object sender, EventArgs e)
    if (txtPassword.Text == "Secret")
        // The password is ok.
        this.DialogResult = DialogResult.OK;
        // The password is invalid.
        MessageBox.Show("Inivalid password.");

This code just compares the password entered by the user to the string “Secret.” If the password matches, the code sets the password form’s DialogResult property to OK. That hides the form and the main program’s call to ShowDialog returns OK.

If the password is invalid, the program clears the password TextBox and displays an error message so the user can try again.

Of course in a real application, you shouldn’t put the correct password in the code. It would be easy for a hacker to look in the code and find the password.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in dialogs, forms, programs and tagged , , , , , , , , , , . Bookmark the permalink.

3 Responses to Display a simple password dialog before a program starts in C#

  1. Asad Ch says:

    Password is commonly used to protect your files. But you have to use the show/hide password option while developing windows form application. That is a great idea i think. Thanks for this great post.

  2. Pingback: Make the user enter a password to use a program in C# - C# HelperC# Helper

  3. Pesek says:

    Thank you very much for this tutorial!
    With slight modifications, it was used in the WPF.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.