Events on CancelButton and FormClose

Wizard for Windows Forms Forum

Posted 17 years ago by Logicway
Avatar
I am using events on OnCancelButtonClick and OnFormClosing.
It seems that OnCancelButtonClick-events fires also the OnFormClosing-event.
        protected override void OnCancelButtonClick(EventArgs e)
        {
            DialogResult cancel = new DialogResult();
            cancel = MessageBox.Show("Are you sure?", "?", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (cancel == DialogResult.OK)
            {
                this.Close();
            }
        }
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            DialogResult close = new DialogResult();
            close = MessageBox.Show("Do you want to exit?", "?", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (close == DialogResult.OK)
            {
                e.Cancel = false;
            }
            else if (close == DialogResult.Cancel)
            {
                e.Cancel = true;
            }
        }
When the user clicks the cancelbutton and clicks OK on the dialog, this fires again a dialog.
How can I prevent this?

Comments (2)

Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Maybe don't show the MessageBox in the Cancel button click event handler. Just call the Close method. That way only the MessageBox in OnFormClosing will show.


Actipro Software Support

Posted 17 years ago by Logicway
Avatar
This is how I made it working.
The FinishButton and CancelButton are calling Application.Exit() and closes the wizard without raising the OnFormClosing event.
        protected override void OnFinishButtonClick(EventArgs e)
        {
            Application.Exit();
        }

        protected override void OnCancelButtonClick(EventArgs e)
        {
            DialogResult cancel = new DialogResult();
            cancel = MessageBox.Show("Do you want to cancel?", "Cancel", 
                     MessageBoxButtons.YesNo, 
                     MessageBoxIcon.Warning, 
                     MessageBoxDefaultButton.Button2);
            if (cancel == DialogResult.Yes)
                Application.Exit();
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                DialogResult close = new DialogResult();
                close = MessageBox.Show("Do you want exit?", "Exit",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Warning,
                        MessageBoxDefaultButton.Button2);
                if (close == DialogResult.Yes)
                    e.Cancel = false;
                else
                    e.Cancel = true;
            }

            base.OnFormClosing(e);
        }

The latest build of this product (v24.1.0) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.