none
Ending a program with hidden forms RRS feed

  • Question

  •  Good afternoon, if anyone can help i would be very glad, i'm doing a software in the visual studio 2012 and i'm having a problem that when i try to open a new form, i put a code saying me.hide, so that you can return to the previous form when you click in it, but when you close one form, the others are still hiding so the program doesnt stop running, should i change the code?
    Wednesday, March 14, 2018 10:03 PM

All replies

  • .......... should i change the code?

    Hi

    Yes.


    Regards Les, Livingston, Scotland

    Wednesday, March 14, 2018 10:12 PM
  • What should i change it to?
    Wednesday, March 14, 2018 10:24 PM
  • What should i change it to?

    Me.Close. That will ensure it isn't still running.   That might have implications for other parts of the application, of course - you can't usually look at forms management in isolation from the overall structure of the application.

    For instance, if your application involves a number of forms that are accessed from one single startup form then that startup form can keep track of what forms it has opened, and close them if the user doesn't.  There are other options.

    Wednesday, March 14, 2018 10:32 PM
  • The problem is that, when i put me.close, the form closes and the other does not show, and the entire program closes, i tried this code:

    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
            Me.Close()
            Form2.Show()

    and the other

    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
            Me.Hide()
            Form2.Show()

    With the second one, i'm able to navigate through the program, but when i close it, it does not stop running, because the other forms are just hidind

    Wednesday, March 14, 2018 10:52 PM
  • The problem is that, when i put me.close, the form closes and the other does not show, and the entire program closes, i tried this code:

    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
            Me.Close()
            Form2.Show()

    They are in the wrong order.  You have to show the next form before closing the existing form.

    With the second one, i'm able to navigate through the program, but when i close it, it does not stop running, because the other forms are just hidind

    Then you need to unhide the original form before you close this form.   To do that you need to be able to refer to the form that you hid.  See here for a way to do that.
    https://vbdotnetblog.wordpress.com/forms/where-did-i-come-from/

    If you want to manage forms like this then you need to establish some procedure such as the one described there so that any form that wants to manage something on another form (eg, its visibility) has available a variable that can be used to refer to that form.   If you are doing anything other than opening and closing forms from a single startup form, then it becomes very complicated and a redesign might be required.

    Wednesday, March 14, 2018 11:09 PM
  • The problem is that, when i put me.close, the form closes and the other does not show, and the entire program closes, i tried this code:

    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
            Me.Close()
            Form2.Show()

    and the other

    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
            Me.Hide()
            Form2.Show()

    With the second one, i'm able to navigate through the program, but when i close it, it does not stop running, because the other forms are just hidind

    This indicates that you may be using default form instances.  That is not recommended, but is another issue.

    Here's something that will work, but it is not the best solution because it does create some duplicated code.  However, it may be an easy solution until you understand more about how to manipulate forms.

    You can paste this code into each of your forms:

    Private Sub Form_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
        Dim openVisibleFormCount As Integer = (Aggregate f As Form In My.Application.OpenForms Where f.Visible = True Into Count)
        If openVisibleFormCount = 0 Then
            For i As Integer = My.Application.OpenForms.Count - 1 To 0 Step -1
                My.Application.OpenForms(i).Close()
            Next
        End If
    End Sub
    

    This code looks through all of the open forms in the application and checks to see if none of them are visible.  If none of them are visible it will loop through each and close them.

    I would still suggest that you follow Acamar's advice and rethink the way you are handling your forms.  This answer is more of a stop-gap to get you going.  It is not a good long-term solution to be used in future projects.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, March 14, 2018 11:31 PM
    Moderator
  • You may need to look at the applications properties window and set the application to exit when the last form closes also.

    La vida loca

    Wednesday, March 14, 2018 11:53 PM
  • I see, i'll try to learn how to correct it, thanks a lot for your help and time!!
    Thursday, March 15, 2018 12:11 AM
  • Ok, Thanks a lot for the help and for your time!
    Thursday, March 15, 2018 12:11 AM
  • Ok, thanks!!
    Thursday, March 15, 2018 12:12 AM
  • You may need to look at the applications properties window and set the application to exit when the last form closes also.

    If OP uses project applicaton settings then the required setting for this case would be When startup form closes, not When last form closes.

    Thursday, March 15, 2018 12:29 AM
  • Hello,

    If your issue has been solved, please remember to close your thread by clicking mark as answer to close your thread, it is beneficial to other community members who face the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 15, 2018 2:41 AM
    Moderator
  • You may need to look at the applications properties window and set the application to exit when the last form closes also.

    If OP uses project applicaton settings then the required setting for this case would be When startup form closes, not When last form closes.


    O.K. So the OP has a different Form to open and closes the startup Form and the application closes. Works for me.

    La vida loca

    Thursday, March 15, 2018 9:12 PM