I have 2 forms right now Form1 and Form2 the default setting start up is Form2. Inside Form 2 there was a button that will release the Form1 button using Form1.Show().

I have now the problem. What I want to do now is to close the Form2 without closing the Form1.

inside Form2 button:

Button_Click....
Form1.Show()
'End
End sub

is there any way that solves my problem?

Dani AI

Generated

As showed, the reason closing Form2 also ends the program is that your application is currently tied to the startup form. As suggested, the simplest immediate fix is to hide the startup form instead of closing it, but that keeps the form alive in memory. Below are practical alternatives and a short, ready-to-use ApplicationContext pattern that gives explicit control over which forms keep the app running.

A few safe options to consider:

  • Quick: call Me.Hide() in Form2 before showing Form1. Simple, but remember to Dispose the hidden form later if you no longer need it.
  • Project setting: in the VB project properties (Application tab) enable the application framework and set “Shutdown mode” to “When last form closes.” That makes the process exit only after every form is closed.
  • Robust: use a custom ApplicationContext or Sub Main to run the message loop yourself. This avoids relying on a single “main” form and lets you open/close forms without prematurely ending the app.

Example ApplicationContext (VB.NET) — put this in a module/class and set Sub Main as the startup object:

Public Class AppContext
    Inherits ApplicationContext

    Public Shared Instance As AppContext
    Private startup As Form2
    Private mainForm As Form1

    Public Sub New()
        Instance = Me
        mainForm = New Form1()
        startup = New Form2()
        AddHandler startup.FormClosed, AddressOf OnFormClosed
        AddHandler mainForm.FormClosed, AddressOf OnFormClosed
        startup.Show()
    End Sub

    Public Sub ShowMainForm()
        If mainForm.IsDisposed Then mainForm = New Form1()
        mainForm.Show()
        startup.Hide() ' or startup.Close() when you want it disposed
    End Sub

    Private Sub OnFormClosed(sender As Object, e As FormClosedEventArgs)
        If Application.OpenForms.Count = 0 Then ExitThread()
    End Sub
End Class

<STAThread>
Sub Main()
    Application.EnableVisualStyles()
    Application.SetCompatibleTextRenderingDefault(False)
    Application.Run(New AppContext())
End Sub

Notes and cautions: avoid using End (it force-terminates the process and can leak resources). If you use Hide, make sure you Dispose the form when finished. If you prefer minimal change, follow and hide Form2; if you want a clean, maintainable solution that scales, use Project Shutdown mode or the ApplicationContext approach above. Thanks to for the visual aid.

Recommended Answers

All 4 Replies

You cant. The Form2 was stared as the Main form in the Main method. You can only Hide it (use this.Hide() method), if you will close it, the whole application will close.

here is a picture can help you

There is maybe another solution:

Private Shared Sub Main()
	Application.EnableVisualStyles()
	Application.SetCompatibleTextRenderingDefault(False)
	Application.Run(New Form1())
	' Form1 in this Application.
	Application.Run(New Form2())
	' Form2 in this application.
End Sub

.. if you want Form2 to open after Form1 exits. There's no going back. When Form2 closes, the programs end.

or:

Imports System.Windows.Forms
Imports Microsoft.VisualBasic.ApplicationServices

Namespace WindowsApplication1
	Public Class MyApp
		Inherits WindowsFormsApplicationBase
		Public Sub New()
			Me.ShutdownStyle = ShutdownMode.AfterAllFormsClose
			Me.MainForm = New Form1()
		End Sub
	End Class
	NotInheritable Class Program
		Private Sub New()
		End Sub
		<STAThread> _
		Private Shared Sub Main(args As String())
			Dim app As New MyApp()
			app.Run(args)
		End Sub
	End Class
End Namespace

You can now close your startup form (Form1) without problems. Your program will exit when the last form is closed.

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.