Question

I have a dialog that I want to prevent from closing when the OK button is clicked, but it returns, and that even if the AcceptButton property is set to none instead of my OK button. What is the best way to stop it from closing?

Was it helpful?

Solution

In fact you are changing the wrong property. You certainly do want AcceptButton to be the OK button. This property determines which is the default button in Windows terms. That is the button which is pressed when you hit ENTER on your keyboard. By changing AcceptButton you are simply breaking the keyboard interface to your dialog. You are not influencing in any way what happens when the button is pressed.

What you need to do is set the DialogResult property of your button to DialogResult.None since that's what determines whether or not a button press closes the form. Then, inside the button's click handler you need to decide how to respond to the button press. I expect that, if the validation of the dialog is successful, you should close the dialog by setting the form's DialogResult property. For example

private void OKbuttonClick(object sender, EventArgs e)
{
    if (this.CanClose())
        this.DialogResult = DialogResult.OK;
}

OTHER TIPS

The best way to stop this behavior is changing the DialogResult property of your OK button to DialogResult.None in the property window at design time.

Also, If you have already some code in the click event of the OK button you could change the form DialogResult.

private void comOK_Click(object sender, EventArgs e)
{
    // your code .....

    // Usually this kind of processing is the consequence of some validation check that failed
    // so probably you want something like this
    if(MyValidationCheck() == false)
    {
        // show a message to the user and then stop the form closing with
        this.DialogResult = DialogResult.None;
    }
}

You need to remove the DialogResult of the button itself as well, in the properties window on the button set it to None.

http://msdn.microsoft.com/en-us/library/system.windows.forms.button.dialogresult.aspx

If the DialogResult for this property is set to anything other than None, and if the parent form was displayed through the ShowDialog method, clicking the button closes the parent form without your having to hook up any events.

Obviously, now your button won't do anything so you will need to register a handler for the Click event.

The best practice is to actually set the Ok button to be disabled rather than not respond to user input.

The DialogResult property SHOULD be set to Ok or Yes depending on the form and the AcceptButton should also be linked to Ok.

I normally create a function on all dialogs and call it whenever the user interacts with the data.

void RefreshControls() { button.Enabled = this.ValidateInput(); }

 static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Form2 fLogin = new Form2();
            if (fLogin.ShowDialog() == DialogResult.OK)
            {
                Application.Run(new Form1());
            }
            else
            {
                Application.Exit();
            }
        }
    }

public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void btnKlik_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
    }

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top