This is my first post. I am a novice in VB.NET. I am able to hide form1 from from2. But I am not able to close the form1 or exit the application from form2. Is there any way in which I can exit the application except from the startup form i.e., form1. Thanks in advance.

Dani AI

Generated

Short summary: hid Form1 and showed Form2, then wanted Form2 to end the whole app. rightly pointed to the framework exit call and showed directly closing the startup form from Form2. Both approaches work, but there are cleaner, more maintainable options and one important project setting to check.

If you’re using the VB Application Framework, change the shutdown behavior so the process ends when the last form closes (Project → [YourProject] Properties → Application → Shutdown mode → choose “When last form closes”). With that set, hiding the startup form and then closing Form2 will terminate the application without explicitly closing the hidden startup form.

A decoupled, safer pattern is to have Form2 signal that the app should exit and let Form1 decide how to respond. Example event-based approach:

' In Form2
Public Event RequestExit As EventHandler

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    RaiseEvent RequestExit(Me, EventArgs.Empty)
End Sub

' In Form1 (when creating Form2)
Dim f2 As New Form2()
AddHandler f2.RequestExit, Sub(s, e) Me.Close()
f2.Show()
Me.Hide()

That avoids tight coupling (Form2 referring directly to the startup form) and keeps shutdown logic in one place.

Notes and troubleshooting:

  • If you simply Hide() the startup form, it still counts as “open” unless your shutdown mode is “When last form closes.”
  • Avoid abruptly terminating the process (use it only as last resort) because it can skip normal closing events and cleanup.
  • If forms run on different UI threads (rare for simple apps), make sure calls happen on the UI thread (Invoke/BeginInvoke).

This gives you a robust way to close the app from a secondary form while keeping the code tidy and predictable.

Recommended Answers

All 4 Replies

Did you try using Application.Exit(). Call this function when you want the entire application to end.

For more help,

nice piece of code. iam amazed that my faculty did not know this. i found the link useful. thank you NHP.

Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Form1.Close()

End Sub


try this.

alyngill is right , use above mentioned code , it will solve your prob .
Regards

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.