Jonathan Acevedo
Jonathan Acevedo

Reputation: 3

How to stop Windows Form program execution until text box input is given?

I'm building a windows form application that stores employee information records in a database, including SSN. Next to the textbox where the SSN is input is requested, I have a checkbox that when clicked, shows me the full SSN instead of the last four digits. To ensure that only an administrator is accessing this information, I created a prompt form connected to a MS SQL DB that stores an admin password and would like to ask the user for a password for security purposes. Also, i'd like to be able to call this form whenever needed. I successfully implemented it but would like to add a feature that allows for 3 tries. Is there a way to stop the program execution and keep prompting the user for input in a textBox?

output is a variable that stores the result of the 'SELECT' query that gets the password.

confirmation is the Accept Button.

The only option i could think of forcing input was calling a new form. Only problem is, this code is inside the form and my gut tells me that's not the answer to this problem. I must be missing something.

     confirmation.Click += (sender, e) => {

            //If Password is correct.
            if (textBox.Text == output)
            {
                isCorrect = true;
                Pprompt.Close();
            }
            else
            {
                isCorrect = false;
                //While the password is incorrect.
                while (isCorrect == false)
                {
                    //textBox.Text = "";
                    if (textBox.Text == output)
                    {
                        isCorrect = true;
                        Pprompt.Close();
                        break;
                    }

                    tryCount++;

                    if (tryCount == 3)
                    {
                        MessageBox.Show("Access Denied.");
                        break;
                    }
                }
            }
         }        

What I'd like to happen is for the form to keep asking me for input until the try limit is exceeded.

Upvotes: 0

Views: 576

Answers (1)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112289

You cannot have the loop inside the click handler, because the UI freezes while it is running and the user does not get the opportunity make any entries. Process only one entry and process the next when the user clicks the button again.

confirmation.Click += (sender, e) => {
    if (textBox.Text == output) // Password is correct.
    {
        isCorrect = true;
        Pprompt.Close();
    }
    else
    {
        isCorrect = false;
        textBox.Text = "";
        tryCount++;
        if (tryCount == 3)
        {
            MessageBox.Show("Access Denied.");
            Pprompt.Close();
        }
    }
}

Unless you are using multi-threading or async/await, the following is true:

  • Winforms is event based. I.e., if no event handler is running, no code is running.
  • If code (i.e. an event handler) is running, the user interface (UI) is frozen and the user cannot make any input. He cannot enter text or click any buttons, cannot scroll lists and cannot resize or move the window.
  • While an event handler is running, no other event handler will ever be called. I.e., an event handler will never be interrupted by another one. This prevents you from having to deal with multi-threading issues unless you are using multi-threading explicitly.
  • If an event should be fired (e.g. a timer tick) while an event handler (e.g. a button click handler) is running, the execution of the event handler associated to this new event will be deferred until after the first event handler returns.

Upvotes: 1

Related Questions