Results 1 to 8 of 8

Thread: Detect child form closing from parent form

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Posts
    30

    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.

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Detect child form closing from parent form

    Yes, you can use AddHandler to handle the child form's FormClosing or FormClosed event.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Posts
    30

    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.
    Last edited by anuragrawat123; Jan 27th, 2014 at 12:53 PM.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Detect child form closing from parent form

    That would then be the next line of code right after the .ShowDialog...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Posts
    30

    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.

  7. #7
    Addicted Member MetalInquisitor's Avatar
    Join Date
    Sep 2012
    Posts
    143

    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

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width