Hi all,


I want to call a button1 click method inside a close button(which is provided by a form) click of a form. But there is no close button click event of a form.

There is only From_closing event and Form_closed events.

Call it from the Closing event.

Call it from the Closing event.

i did it.

i called butto1.Peformclick. in button1 click shows a msgbox. when i click close button it shows that msgbox. but when i click ok of that msgbox it closes itself and the form too. i want to stop that form to be closed if that condition violates.

this is my code.

private void button1_Click(object sender, EventArgs e)
        {
            if (button3.Enabled==false)
            {
                this.Dispose();
            }
            else
            {
                MessageBox.Show("You must enter data for this form.", "Failed", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                
                           }
        }
.............................................................................
private void BookDetail_FormClosing(object sender, FormClosingEventArgs e)
        {
            button1.PerformClick();
        }

If you don't want the form to close, you need to set the FormClosingEventArgs, as in

e.Cancel = true;

Since you want to use the same code in two methods, you should do something like this:

private void button1_Click(object sender, EventArgs e) {
    if (GetCloseStatus() == false) {
        this.Dispose();
    }
}

private void BookDetail_FormClosing(object sender, FormClosingEventArgs e) {
    e.Cancel = GetCloseStatus();
}

private Boolean GetCloseStatus() {
    if (button3.Enabled) {
        MessageBox.Show("You must enter data for this form.", "Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return true;
    }

    return false;
}
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.