Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Is there a way I can close the parent form by closing the child form?

I tried to search for a possible post about this on the forum but I couldn't find any.

Anyway, is there a way for me to close the parent form by closing the child form?

Ex: I have the first form do its thing and then it opens another form, I wish to hide the parent one because it is no longer in use but doing so would mean there is no way to kill the program once it is done being used? (I want to make it easier and just have the person I'm making this for be able to just exit it, without having to go into task manager to end the program.)

I know I can use Application.Exit() but is there a way for me to make it so that when the person clicks the exit button of the child form it exits the application? (I don't want to make a button and have them click it to terminate the application.)
Yup... here's the link that shows you how to do what you want ;) https://www.daniweb.com/software-develop...rm-is-open Let me know if you still need help ;)


Or you could do something like this:

Private Sub btnOpenForm_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim frm2 As Form2 = New Form2()
AddHandler frm2.FormClosed, AddressOf frm2_FormClosed
frm2.Show()
Me.Hide()
End Sub

Private Sub frm2_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)
Me.Close()
End Sub
(Apr 20, 2015 05:33 PM)nuk Wrote:  Or you could do something like this:

Private Sub btnOpenForm_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim frm2 As Form2 = New Form2()
AddHandler frm2.FormClosed, AddressOf frm2_FormClosed
frm2.Show()
Me.Hide()
End Sub

Private Sub frm2_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)
Me.Close()
End Sub
I'll give this a try, I'm pretty new at this so a lot of this code doesn't really make sense to me (or I just don't really understand what its doing).

Would I have to implement this code in all the forms that come after the parent form?

"Private Sub btnOpenForm_Click(ByVal sender As Object, ByVal e As EventArgs)"
Isn't this code for a button click? Hm, you might not have seen my edit where I said I didn't want to make it so that the person had to click a button to terminate the program once they were done using it. I would like it so that they could just click the 'x' and it'll terminate the parent once they're done.
yes it's a button click but you can just put the code in the 'Form_Closing" event or something like that ;) If you want; I'll be home in a few hours and I can make a demo source with a MdiForm/Parent example with this code for you?
(Apr 20, 2015 08:00 PM)nuk Wrote:  yes it's a button click but you can just put the code in the 'Form_Closing" event or something like that ;) If you want; I'll be home in a few hours and I can make a demo source with a MdiForm/Parent example with this code for you?
That would be greatly appreciated!
From what I've looked up on Google I believe the MdiForm is a form within a form isn't it? I want like an actual child form that is totally separate from the parent.

Say you click a button on Form1 and it continues the program, it opens up Form2 and hides Form1. Exiting Form2 wouldn't close Form1 (exiting as in clicking the red X), I want it to be able to exit the whole program if I were to just exit Form2.
Is that possible?

I tried the Form_Closing that you spoke of but I'm not sure I got the code correct and so when I exited say Form2, Form1 was still running in the background.

EDIT: I searched on Google but couldn't find anything but when I tried Form_Closing instead of a more specific question I found the answer!

Thank you nuk for all your help!


Code
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Application.Exit()
End Sub
(Code was written by Adriaan Stander)
Oh shit... nevermind what I sent you... what you want is very easy. I'm gonna make you something now in like 3min. and I'll post the link for you to download the source then ;)


Here's the download link the the source file example I made for you :)

http://www.2shared.com/file/fgJtHRN3/WildElephant.html
(Apr 20, 2015 09:55 PM)nuk Wrote:  Oh shit... nevermind what I sent you... what you want is very easy. I'm gonna make you something now in like 3min. and I'll post the link for you to download the source then ;)


Here's the download link the the source file example I made for you :)
I appreciate your help! Thank you very much!
(Apr 20, 2015 10:17 PM)WildElephant Wrote:  I appreciate your help! Thank you very much!

Anytime homie, anytime ;) If you need help with anything else; don't hesitate to ask... i mean of course try to figure it out yourself first but we're always here to help ... well most of us anyway LOL ✌


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by Tr0jandogSep 10, 2019 11:47 AM 0 137 Sep 10, 2019 11:47 AM
Last Post: Tr0jandog
[C#] by APΣXDec 22, 2018 09:36 AM 7 259 Dec 22, 2018 09:36 AM
Last Post: APΣX
[C] by chrizatorJul 22, 2018 05:09 AM 4 229 Jul 22, 2018 05:09 AM
Last Post: Anzee