There is a Windows Form application for example say 'A'.

and there is another class called '' that will call 'A' as apart of its action.

When '' calls 'A' , 'A' will pop out, and continue to execute.

what i want to do is, when '' calls 'A' , the code should stop continuing execution until the windows form 'A' is closed.

Hope yourl undestood my question. Basically, i need a form to popup (the form contains a button and a text box), so the user is required to enter some value to the text box and click on the submit button, and then only the code should continue executing its remaining code.

It is like Console.readline . where the execution of the codes remains still until someone types something in


Help appreciated.

Dani AI

Generated

As and correctly pointed out, the right approach is to present the popup as a modal dialog so the caller waits for the user to finish. Below are practical patterns to return the textbox value, validate before closing, and avoid common pitfalls you might not see in the short examples already posted.

Expose the entered text and a submitted flag from the dialog so the caller can read them after the dialog closes. This keeps logic simple and avoids tight coupling:

public partial class SubmitForm : Form
{
    public bool Submitted { get; private set; }
    public string UserInput => textBoxInput.Text;

    private void okButton_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrWhiteSpace(textBoxInput.Text))
        {
            MessageBox.Show("Please enter a value.");
            return;
        }
        Submitted = true;
        Close();
    }
}

In the caller, instantiate and show the dialog modally, then read the properties after it returns. Wrap the dialog in a using to ensure disposal:

using (var dlg = new SubmitForm())
{
    dlg.ShowDialog();    // caller is paused while dialog is open
    if (dlg.Submitted)
    {
        var value = dlg.UserInput;
        // continue processing with value
    }
}

Extra tips: set the form's AcceptButton to your submit button so Enter submits, and CancelButton if you want Esc to close. Use the FormClosing event to prevent accidental closure (e.Cancel = true) until validation or explicit cancel. If the calling code runs on a background thread, marshal the dialog call to the UI thread (Control.Invoke / SynchronizationContext) — WinForms controls should be created and shown on the UI thread. This pattern gives a simple, testable way to block execution until the user supplies input and to safely retrieve that input afterward.

Recommended Answers

All 2 Replies

When you make a new 'A' and tell it to Show(), you should see another method called ShowDialog(). This is exactly what you want as execution will not continue until that dialog is closed.

Here's an example. Assuming the form you want to call is named SubmitForm.

SubmitForm A = new SubmitForm();
A.ShowDialog();
MessageBox.Show("The submit form closed."); //This will not show until the form closes.

If you want the Submmit button to actually close the form. Then put this at the end of your code for it.

this.DialogResult = DialogResult.OK;

All forms shown as 'dialogs' return a DialogResult to the caller. They will close once their DialogResult has been assigned, which is what that code does. You can get its DialogResult by changing line 2 of the above to:

DialogResult SubmitResult = A.ShowDialog();

Now you'd just have to check the value with an IF statement. You don't have to do that though. ;)

Using your example, it would be

A myForm = new A();
A.ShowDialog();  // execution stops here until the user closes the form 'A'
...
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.