Click here to Skip to main content
14,265,982 members
Rate this:
Please Sign up or sign in to vote.
See more:
C# How can I make the application wait until the user enter something in each condition then go to the next condition

What I have tried:

private void button17_Click(object sender, EventArgs e)
            Button btnSender = (Button)sender;
          if (message.TextLength != 0 && message.TextLength < 5 )
              message.Text = "Enter Your Current 4 digits PIN ";

              if (Convert.ToInt32(message.Text) == PIN && btnSender == button14)
                  message.Text = "Enter Your New 4 digits PIN ";
              if( btnSender == button14)
                  PIN = Convert.ToInt32(message.Text);
                  WindowsFormsApplication5.Settings.Default.PinNumber = PIN;
                  message.Text = " Your PIN has changed successfully  ";

              message.Text = "The PIN you have entered is not correct please try again!  ";

          else { message.Text = "Please enter a valied PIN "; }
Rate this:
Please Sign up or sign in to vote.

Solution 1

The whole idea of "waiting" is wrong.

In a Windows application nothing should wait for anything. More generally, in whole programming of multitasking systems, CPU should not also wait for anything; if some thread is in a wait state, it should not waste a single tick of the CPU time which should do something else. But before you understand this, you have to grasp the idea of at least single-threaded event-oriented UI, which works not wasting any CPU resources at all until the hardware interrupt from an input device invokes some event which your application responds to.

As you are missing the whole fundamental programming paradigm, no Quick Question & Answer can help you immediately. Nothing can replace systematic study of the subject.

However, even though your exact requirements are, by far, not clear, I'll try to give you a hint. As I can understand, you expect the user to meet some condition by typing a string value in a text box. Then you can handle the event  System.Windows.Forms.TextBox.TextChanged:
Control.TextChanged Event (System.Windows.Forms)[^].

In the handler of this event, you can take the current Text value of the text box and check up if one or another condition has met. In this case, you perform the next action.

However, I would strongly recommend not doing it this way. Or, as a maximum, you can provide a user with some visual feedback of having the condition met. Typically, it could be the enabled state of some button or other control (which would be grayed out if the condition is not met, and not Enabled), but it could be, say, change in some color. If you take some more active action, it can be too confusing to a user. Besides, you can perform final validation of all entered data just before it is about to be used. In other words, don't stress the user and limit the user's actions too intrusively.

Besides, never call Convert methods what parsing can be used. Instead use
int pin;
if (int.TryParse(message.Text, out pin))

Rate this:
Please Sign up or sign in to vote.

Solution 2

When you absolutely require a valid entry from a user before proceeding to some other mode, or action, there are several strategies:

a. "wizard UI:" show one Form at a time for each entry requirement; and:

a.1. if you want to give the user some limited number of attempts to enter a valid value: write the code for that.

a.2. or, allow the use to "go back" to a previous "step."

a.3. you could use a TabControl on one Form as the basis for a wizard, or, a series of Panels on one Form, or, you could use a series of Forms.

b. "all entry fields on one Form:" you could use validation, and enforcing focus/capture when a field is not validated for each field. this can be complex because you will need to write 'Leave and 'Enter EventHandlers for all the Controls, and handle when the user clicks the Cancel Button.

Hopefully, you will do the right thing, so the user never sees the "Submit" Button enabled unless all validation has occurred.

b.1. for example, I enter an incorrect value in AgeEntryTextBox;

b.1.1 your validation code flags the entry as an error, and: if the user then attempts to do "something else" on the Form besides clicking the Cancel Button on the Form:

b.1.1.1.a. you detect the user trying to do "something else" in the 'Leave EventHandler for the TextBox.

b.1.1.1.b. you set a boolean Flag in the leave event, and you then detect that flag if the user tries to 'Enter any other Control on the Form but the Cancel Button, or, the "Submit" Button if it is enabled.

b.1.1.1.c so, the user needs to re-enter their data in the 'AgeEntryTextBox: you clear 'AgeEntryTextBox, set the boolean flag to false, call AgeEntryTextBox.Focus(), and set AgeEntryTextBoxCapture Property to 'true

If you use Forms shown modally ('ShowDialog), you will, of course, need to deal with returning validated user input to your calling context.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100