Dave Love
Dave Love

Reputation: 31

Problem when using the "using" keyword

I'm using the "using" keyword to load a dialog where the user enters the captcha code from a webpage, but the problem is the dialog is loading and closing instantly. How can I ensure the dialog waits for user input?

using (Form2 captchaDialog = new Form2(captchaImage))
{
    captchaDialog.Show();
    captchaText = captchaDialog.captcha;
}

Upvotes: 3

Views: 122

Answers (2)

Michael Todd
Michael Todd

Reputation: 17051

You could use ShowDialog method instead:

using (Form2 captchaDialog = new Form2(captchaImage))
{
    captchaDialog.ShowDialog();
    captchaText = captchaDialog.captcha;
}

Upvotes: 7

Pierre-Alain Vigeant
Pierre-Alain Vigeant

Reputation: 23083

Your form is closing immediately because your code hits the end of the using block which will dispose the newly created form, which in turn close the form if it is open. You have to block your call to the dialog by using ShowDialog.

using (var dialog = new Form2(captchaImage))
{
    if(dialog.ShowDialog() == DialogResult.OK)
    {
        captchaText = captchaDialog.captcha;
    }
}

Upvotes: 3

Related Questions