Link to home
Start Free TrialLog in
Avatar of ipjyo
ipjyoFlag for United States of America

asked on

How to keep the focus back to form when clicking on OK button?

I have a windows form with multiple controls and OK and Cancel buttons. In the Click event handler of OK button, it checks to see if there are errors on the form. I need the form to stay open if there are errors. But the code below is not working. If there are errors, it is closing the form and returning to the caller. And I have the below two lines to invoke and show the form.

PtForm paymentForm = new PtForm();
ptForm.ShowDialog();


Thanks for any help.
private void btnOk_Click(object sender, EventArgs e)
        {
            this.ValidateChildren(ValidationConstraints.Visible);
            string ErrorString = GetValidationErrors();
            MessageBox.Show(ErrorString, "Errors");

            if (!string.IsNullOrEmpty(ErrorString))
            {
                return;
            }

	//Processing

	}

Open in new window

SOLUTION
Avatar of ToddBeaulieu
ToddBeaulieu
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Be careful with the above code though. You don't want to prevent the user from canceling, which that does as it's written.
Avatar of ipjyo

ASKER

Thanks for the response. Actually I verified that the form's accept button is set to none.



SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of nishant joshi
nishant joshi
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ipjyo

ASKER

yeah..DialogResult was set to OK in the designer and that was the culprit.

Thank you very much.