Hi guys,

Currently I am developing a window forms application using VB.NET. Yet, I have a problem in forms navigation.

The problem is :
Now I have created 3 forms. Let say form 1, form 2 and form 3.
Now, I want to navigate from form 1 to form 2, and from form 2 to 3 then form 3 back to form 1 again. Its time i navigate to the next form..i want to make sure that the previous form is closed.

In this case, Do you guys have any solutions for it?

Dani AI

Generated

A few concise, reliable ways to do sequential navigation (Form1 -> Form2 -> Form3 -> back to Form1) in WinForms, especially useful if you are on older Visual Studio like VS2003 and do not have the newer project-level shutdown option mentioned by . The core idea is to control the app message loop yourself so closing a form does not unintentionally exit the app.

A recommended pattern: a small ApplicationContext that owns the current form and opens the next one when the current form closes. This keeps the message loop alive until you explicitly call ExitThread.

Public Class NavContext
  Inherits ApplicationContext

  Private currentForm As Form

  Public Sub New()
    ShowForm(New Form1())
  End Sub

  Private Sub ShowForm(f As Form)
    If currentForm IsNot Nothing Then
      RemoveHandler currentForm.FormClosed, AddressOf OnClosed
      currentForm.Close()
    End If
    currentForm = f
    AddHandler currentForm.FormClosed, AddressOf OnClosed
    currentForm.Show()
  End Sub

  Private Sub OnClosed(sender As Object, e As FormClosedEventArgs)
    If TypeOf sender Is Form3 Then
      ExitThread()
    ElseIf TypeOf sender Is Form1 Then
      ShowForm(New Form2())
    Else
      ShowForm(New Form3())
    End If
  End Sub
End Class

Use a Sub Main as the startup object and run the context:

Public Module Program
  Public Sub Main()
    Application.Run(New NavContext())
  End Sub
End Module

Alternatives: open forms modally with ShowDialog for strict sequential flow (use DialogResult to decide next), or call Hide() on the current form and Show() the next, then close the hidden one later. Beware: closing the startup form will end the app unless you control the message loop (ApplicationContext / Sub Main) or use modal dialogs.

Further reading: ApplicationContext and Form.ShowDialog. These patterns avoid apps exiting unexpectedly and give explicit, maintainable control over form navigation.

Recommended Answers

All 4 Replies

I am using vb 2005 express.
Double click on the "My Project" in Solution Explorer. Under the Application tab, lower left hand side, you will see Shutdown Mode. In the dropdown box select "When last form closes".
Now in a button in form1 put

Form2.Show()
Me.Close()

You will do the same thing in form2 and form3.
Notice the order. If you do Me.Close first the program ends, so, always open the next form before closing the current form.

Thanks for your reply, I couldn't find the shutdown mode when I followed the procedure given by you. I think is because I am using Microsoft Visual Studio .NET 2003 not 2005.

So any idea about it regarding of the problem I posted previously?

I think "Shutdown Mode" is new to vbe 2005. Try the code anyway.

I will do further researchs abou the code, Anyway thanks for your helps! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.