Windows Develop Bookmark and Share   
 index > Windows Forms General > Form cancel button
 

Form cancel button

I have two forms:

formA and formB. formA shows formB

formB has a button that is identified on the form as the cancel button.

Is there a way in formA to know when the formB.cancel button was clicked?

Or should I set my own boolean flag?
Can-Ann  Wednesday, April 02, 2008 6:19 PM
Can-Ann wrote:

I have two forms:

formA and formB. formA shows formB

formB has a button that is identified on the form as the cancel button.

Is there a way in formA to know when the formB.cancel button was clicked?

Or should I set my own boolean flag?

If FormA is an MdiParent, then you cannot use ShowDialog(). The above suggestions may run into problems with a child form andits parent.

Add this to the child form.

Code Snippet

// Declare a custom delegate for custom custom events.

internal delegate void Form2Event(object sender, EventArgs e);

// Give this event a name that reflects what occurs.

internal event Form2Event CloseButtonClickedEvent;

protected override void OnClosing(CancelEventArgs e)

{

e.Cancel = true; // set to false to allow form to close.

base.OnClosing(e);

CloseButtonClickedEvent.Invoke(this, e);

}

Try this when you load the child form inside of a button click event method.

Code Snippet

Form2 form2 = new Form2();

form2.MdiParent = this;

form2.Show();

form2.CloseButtonClickedEvent +=

new Form2.Form2Event(form2_CloseButtonClickedEvent);

and add this method to the MdiParent form.

Code Snippet

void form2_CloseButtonClickedEvent(object sender, EventArgs e)

{

MessageBox.Show("User clicked Close Form button on child Form2.");

}

Hope this helps.

Rudedog

Better add this stuff to the child form, Form2, to make sure that event isn't a null when it is called.

Code Snippet

private void Form2_Load(object sender, EventArgs e)

{

this.CloseButtonClickedEvent += new Form2Event(Form2_CloseButtonClickedEvent);

}

void Form2_CloseButtonClickedEvent(object sender, EventArgs e)

{

// Do nothing. This method insures that the InvocationList is not null

}

Rudedog2  Wednesday, April 02, 2008 8:46 PM

In formA do the following:

FormB b = new FormB();

DialogResult rslt = b.ShowDialog();

if (rslt == Dialogresult.Cancel) //-- This tells you right here whether or not it was cancelled.

Micah Martin  Wednesday, April 02, 2008 6:24 PM

If you declare the button you want to monitor as public, you could do something like this:

Code Snippet

public void ShowForm() {

Form2 form2 = new Form2();

form2.button1.Click += new EventHandler(button1_Click);

form2.Show();

}

void button1_Click(object sender, EventArgs e)

{

// handle the button click event here.

}

You can either change the button to public in the Designer code towards the bottom:

Code Snippet

public System.Windows.Forms.Button button1;

Or you can change the "Modifiers" property of the button in the designer to "Public".
David M Morton  Wednesday, April 02, 2008 6:29 PM
given that in formB:
Code Snippet

btnCancel.DialogResult = DialogResult.Cancel;




then form A can capture the result like:

Code Snippet

//the button click that instantiates formB
private void btnCreateFormBClick(object sender, EventArgs e)
{
frmB formB;
try
{
formB = new frmB();
frmB.ShowDialog();
if (frmB.DialogResult == DialogResult.Cancel)
{
MessageBox.Show("You clicked cancel");
}
}
catch...

}




omarazam  Wednesday, April 02, 2008 6:36 PM
Can-Ann wrote:

I have two forms:

formA and formB. formA shows formB

formB has a button that is identified on the form as the cancel button.

Is there a way in formA to know when the formB.cancel button was clicked?

Or should I set my own boolean flag?

If FormA is an MdiParent, then you cannot use ShowDialog(). The above suggestions may run into problems with a child form andits parent.

Add this to the child form.

Code Snippet

// Declare a custom delegate for custom custom events.

internal delegate void Form2Event(object sender, EventArgs e);

// Give this event a name that reflects what occurs.

internal event Form2Event CloseButtonClickedEvent;

protected override void OnClosing(CancelEventArgs e)

{

e.Cancel = true; // set to false to allow form to close.

base.OnClosing(e);

CloseButtonClickedEvent.Invoke(this, e);

}

Try this when you load the child form inside of a button click event method.

Code Snippet

Form2 form2 = new Form2();

form2.MdiParent = this;

form2.Show();

form2.CloseButtonClickedEvent +=

new Form2.Form2Event(form2_CloseButtonClickedEvent);

and add this method to the MdiParent form.

Code Snippet

void form2_CloseButtonClickedEvent(object sender, EventArgs e)

{

MessageBox.Show("User clicked Close Form button on child Form2.");

}

Hope this helps.

Rudedog

Better add this stuff to the child form, Form2, to make sure that event isn't a null when it is called.

Code Snippet

private void Form2_Load(object sender, EventArgs e)

{

this.CloseButtonClickedEvent += new Form2Event(Form2_CloseButtonClickedEvent);

}

void Form2_CloseButtonClickedEvent(object sender, EventArgs e)

{

// Do nothing. This method insures that the InvocationList is not null

}

Rudedog2  Wednesday, April 02, 2008 8:46 PM

You can use google to search for other answers

Custom Search

More Threads

• Datagridview tab control
• How to add parents, children to a treeview
• Using Win32API to check scroll information from text box
• How can I get a screenshot of the current Window ?
• Opening existing Forms?
• How to do form authentication and authorization in my WinForms app?
• Print ZPL codes to ZEBRA printer using PrintDocument class
• FileDialog and Listbox
• audio playback device.?
• office 2003 icons...