Home > Net >  How to make a program wait for user to type in a textbox?
How to make a program wait for user to type in a textbox?

Time:08-13

When I run a WinForm program to a line, I would like to check if a textbox already has user input, if not, I will ask user to type in the textbox and wait till user types in some input, before running the next line of the code. I was wondering how to do the wait?

The program has to wait for the information required as input for the next line of code.

Thanks.

CodePudding user response:

Waiting for something to happen in the GUI (using a timer, loops from other threads, etc...) is a massive waste of resources.
Almost all functional programming languages have Events including C#

From Wikipedia:

event-driven programming is a programming paradigm in which the flow of the program is determined by events such as user actions (mouse clicks, key presses), sensor outputs, or message passing from other programs or threads. Event-driven programming is the dominant paradigm used in graphical user interfaces and other applications (e.g., JavaScript web applications) that are centered on performing certain actions in response to user input. This is also true of programming for device drivers (e.g., P in USB device driver stacks).

You can do it like this with the help of Control.TextChanged event inherited by the Textbox control:

private void Form1_Load(object sender, EventArgs e)
{
    ValidateGUI();
}

private const int MIN_CHARS_TO_DO_SOMETHING = 8;
private const string NOT_VALID = "Oh No There is No User Input )-:";
private const string VALID = "Great We Can Do Something (-:";

private void textBox1_TextChanged(object sender, EventArgs e)
{
    ValidateGUI();
}

private void ValidateGUI()
{
    if (textBox1.Text.Length < MIN_CHARS_TO_DO_SOMETHING)
    {
        lblMessege.Text = NOT_VALID;
    }
    else
    {
        lblMessege.Text = VALID;
        // Execute some code..
        //...
        //...                  
    }
}

CodePudding user response:

I'm going to assume you have a valid reason for waiting instead of monitoring input.

You just need to use background workers and then you need to set the DoWork event to wait for a specified amount of time, and the RunWorkerCompleted event to run your code checking if input is being made.

Here's an example assuming a label and a textbox are on the form already. Alternatively you can just add the background worker as a form element instead of creating it in code:

    public Form1()
    {
        InitializeComponent();
        waitForInput();
    }
    
    private void waitForInput()
    {
        BackgroundWorker waiter = new BackgroundWorker();
        waiter.WorkerSupportsCancellation = true;
        waiter.WorkerReportsProgress = true;
        waiter.DoWork  = wait10Seconds;
        waiter.RunWorkerCompleted  = doneWaiting;
        waiter.RunWorkerAsync();
        
    }

    private void wait10Seconds(object sender, DoWorkEventArgs e)
    {
        System.Threading.Thread.Sleep(10000);
    }
    private void doneWaiting(object sender, RunWorkerCompletedEventArgs e)
    {
        if(textBox1.Text == "")
        {
            label1.Text = "Why haven't you typed anything?";
        }
    }
  • Related