Detect child form closing from parent form
I have a windows app with a parent form and a child form which opens on click event of a button in parent form.
There is a button OK in child form.
When I press OK child form closes. I want to do some logic on parent form when child form closes immediately.
Can I track down in parent form with the help of any event or property like it was previouspage method in asp.net.
Re: Detect child form closing from parent form
Yes, you can use AddHandler to handle the child form's FormClosing or FormClosed event.
Re: Detect child form closing from parent form
Are you displaying the child form with Show or ShowDialog? Presumably Show but your question suggests that perhaps ShowDialog could be more appropriate. Do you want the user to be able to access the parent even while the child is open?
Re: Detect child form closing from parent form
I am using ShowDialog to open child form. On click of OK button on child form the form is closed. Now I want to catch in Parent form that the child form is just closed.
Re: Detect child form closing from parent form
That would then be the next line of code right after the .ShowDialog...
-tg
Re: Detect child form closing from parent form
I am doing this only. But in child form I have OK, Cancel two buttons.. and i want to differentiate between them when i close child form and reach on parent form.
i.e. which button was clicked on child form.
Re: Detect child form closing from parent form
On the OK and Cancel buttons on your child form, set their respective DialogResult properties to OK and Cancel, and then from your parent form you can do something like:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim childForm As New Form2
Dim dr As DialogResult = childForm.ShowDialog()
If dr = DialogResult.OK Then
MessageBox.Show("Clicked OK")
Else
MessageBox.Show("Clicked Cancel")
End If
End Sub
Re: Detect child form closing from parent form
Perhaps you could have mentioned that from the start. In the form... your child form. Go to its properties... find the AcceptButton and the CancelButton properties... set them to the appropriate OK & Cancel buttons that are on your form... then in your code in the parent form where you .ShowDialog your form... capture the result of the call to the ShowDialog...
like this:
Code:
Dim myChildForm As New Form3
Dim childResult As DialogResult = myChildForm.ShowDialog()
If childResult = Windows.Forms.DialogResult.OK Then
MessageBox.Show("OK was clicked!")
Else
MessageBox.Show("Cancel was clicked")
End If
-tg