Solved

Manage Windows Forms .NET

Posted on 2004-10-15
14
295 Views
Last Modified: 2013-12-03
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
Comment
Question by:Hans Langer
  • 7
  • 4
  • 2
  • +1
14 Comments
 
LVL 9

Expert Comment

by:Lacutah
ID: 12324936
Add a handler to the OnClose event of Forms 2 and 3, something like:

Private Sub Form2_OnClose()
    Form1.Show()
End Sub
0
 
LVL 10

Author Comment

by:Hans Langer
ID: 12324942
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
 
LVL 10

Author Comment

by:Hans Langer
ID: 12324956
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12324965
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
 
LVL 9

Expert Comment

by:Lacutah
ID: 12324990
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
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12325006
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
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12325036
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
 
LVL 10

Author Comment

by:Hans Langer
ID: 12325091
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
 
LVL 9

Expert Comment

by:Lacutah
ID: 12325116
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
 
LVL 10

Author Comment

by:Hans Langer
ID: 12325128
its open by default.
just -> property -> inicial object -> form1
0
 
LVL 10

Author Comment

by:Hans Langer
ID: 12325129
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
 
LVL 9

Accepted Solution

by:
Lacutah earned 100 total points
ID: 12325181
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
 
LVL 10

Author Comment

by:Hans Langer
ID: 12325334
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
 
LVL 10

Author Comment

by:Hans Langer
ID: 12325384
Its ok, Lacutah Thanx, And thanx to all.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Extracting last characters in a substring 5 26
Can I print examples of all of my installed fonts? 6 20
VB.NET 1 21
Create a datatable in vb.net dynamically 1 12
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question