Hi,

I am pretty new to C# and cannot seem to find an answer to this question after 10 minutes google searching. What is the best way to suspend a program to wait for input on a textbox, specifically enter. I think my current problem is that I am coding inside the form, so if I want to do a block on it (like a semaphore) I end up suspending the textbox inside the form as well. I attempted to make a thread that would handle the main execution, but it does not have direct access to the form since it did not create it. I am basically looking for something with similar logic to cin in c++. Thanks.

Recommended Answers

All 2 Replies

It appears your are used to procedural programming (where each line is executed one after another, input stops the system, etc.) What you need to learn is event driven programming (which is what all GUI based systems are doing).

You don't 'stop' the execution. You set up the GUI and you attache your code to 'events' such as 'button click', 'mouse over'. In your case, you'd want something to indicate that the user is done with entering text in the textbox (Enter key pressed, focus changes out of the textbox, submit button) then enter the code you need to handle the input in that event.

I've attached a small program that shows this. You'll want to look at the Form1.cs file to see how the events are handled (the two buttons).

The cin "analog" in C# is Console.ReadLine.
But as you are using textboxes in a FormsApplication, you cannot use that, you have to write a ConsoleApplication.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.