Solved

Manage Windows Forms .NET

Posted on 2004-10-15
14
277 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 10

Author Comment

by:Hans Langer
Comment Utility
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
Comment Utility
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
Comment Utility
its open by default.
just -> property -> inicial object -> form1
0
 
LVL 10

Author Comment

by:Hans Langer
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Its ok, Lacutah Thanx, And thanx to all.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now