Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Manage Windows Forms .NET

Posted on 2004-10-15
14
Medium Priority
?
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

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 300 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

610 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