• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

Manage Windows Forms .NET

I have 3 Forms
Form1, Form2,Form3

In Form1 I have a button that open Form2 and hide Form1, the same with Form2 and Form3.
All forms have Back button that close and show forms

My problem is that when I Close Form3 or Form2 the application dont finish, because the other form still loaded but hidded.

Thanks.
0
Hans Langer
Asked:
Hans Langer
  • 7
  • 4
  • 2
  • +1
1 Solution
 
LacutahCommented:
Add a handler to the OnClose event of Forms 2 and 3, something like:

Private Sub Form2_OnClose()
    Form1.Show()
End Sub
0
 
Hans LangerAuthor Commented:
Thanks,
But the problem its if I press back in form3 its gonna close form3 (and show form2) and with those events its gonna close the application.
0
 
Hans LangerAuthor Commented:
There is 2 instance when the Form2 and Form3 close.

1.- When the User click de "X" on the control box
2.- When the user press back to the Previous Form

I want close the app only when the user close with the first way
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' go to form2
        Dim f2 As New Form2
        AddHandler f2.Closed, AddressOf f2_Closed
        Me.Hide()
        f2.Show()
    End Sub

    Private Sub f2_Closed(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.Show()
    End Sub
End Class

Public Class Form2
    Inherits System.Windows.Forms.Form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' go back to form1
        Me.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ' go to form3
        Dim f3 As New Form3
        AddHandler f3.Closed, AddressOf f3_Closed
        Me.Hide()
        f3.Show()
    End Sub

    Private Sub f3_Closed(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.Show()
    End Sub
End Class

Public Class Form3
    Inherits System.Windows.Forms.Form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' go back to form2
        Me.Close()
    End Sub
End Class
0
 
LacutahCommented:
On the  btnBack_ click event handler do the following instead:

Private Sub btnBack_Click(sender as object, e as EventArgs) handles btnBack.Click()
   'Go ahead and close me, OnClose will open the prior form.
   me.Close()
end sub
0
 
eozz_2000Commented:
In Form2 and Form3

private frm as Form
Public Overloads Sub Show(byRef frm as Form)
  me.frm = frm
  me.frm.Hide()
  me.Show()
End Sub

In the closing event of Form2 and Form3:

me.frm.Show()
me.Close()
0
 
eozz_2000Commented:
When you want to show Form2 and Form3 you must do with this code:

dim frm as new Form2()
frm.Show(me)

and

dim frm as new Form3()
frm.Show(me)
0
 
Hans LangerAuthor Commented:
There is 2 instance when the Form2 and Form3 close.

1.- When the User click de "X" on the control box
2.- When the user press back to the Previous Form

I want CLOSE THE APPLICATION when the user close with the first way
For the second way those code works.
0
 
LacutahCommented:
Are you using a generic class to open the first form?  (ie, sub Main?) or is the first form being opened by default?  This will change the most practical solution.
0
 
Hans LangerAuthor Commented:
its open by default.
just -> property -> inicial object -> form1
0
 
Hans LangerAuthor Commented:
In form2 I have

Public Sub New(ByVal form1 As form1)
me.frmParent = form1
.....

and in the back button i have,

me.frmParent.Show()
Me.Close()

But if I use the closing event  I cant know if he want to go back or is closing the app
0
 
LacutahCommented:
OK, good!

In form2 & 3, add a boolean variable to the form class:

dim CloseApp as boolean = True

In the Back Button (form2 and 3) event, set CloseApp to False
private sub btnBack_Click(...)
   ....
   CloseApp = False
   me.frmParent.Show()
   Me.Close()
end sub

In the Form2/3 Close Event:
Private Sub Form2_Close(...)
    If CloseApp then
        me.frmParent.Close
    end if
end sub

This should (assuming Form3 is opened from From2, and form2 is opened from Form1) cascade, otherwise you'll need to keep references in form1 to all open forms and code it's OnClose event handler similairly.
0
 
Hans LangerAuthor Commented:
I looking for an optimized way.
If I have 10 forms I have to add

dim CloseApp as boolean = True
Private Sub Form2_Close(...)
    If CloseApp then application.exit()
end sub

In all forms!

I this this way is better but I dont know how to do it, plis help

When I press next button I Show the previous Form and then , in the closing event, I Ask for each form, if all forms are hidden (except Me) then I close the application.

How I ask for Each Form In the  app ? and if they are hidden ?
0
 
Hans LangerAuthor Commented:
Its ok, Lacutah Thanx, And thanx to all.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now