Hi,
i"m creating an application involving multiple forms i"ve successfully linked the forms but the problem is when
a new form(form2) is linked by a button control form1 is also running,so i placed the following code in the load event of form2 "form1.close()" this segment of code terminates the entire program when executed. how can i create an application with multiple forms but a single form will be displayed at the moment unless the users enters into a new form....

Dani AI

Generated

Short answer: the runtime stops when the form that drives the message loop goes away. That explains why your app exits when the form you closed was the startup form (as described). Other replies here (thanks to , and @bhi) point to quick fixes; below is a robust alternative that avoids making any single form the “application lifeline.”

Use a custom ApplicationContext so the application lifetime is managed by a small controller object rather than by one form. The controller can open, swap or close forms and only calls ExitThread when no windows remain. Example (VB.NET):

Public Class AppContext
    Inherits ApplicationContext

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

    Public Sub ShowForm(frm As Form)
        AddHandler frm.FormClosed, AddressOf OnChildClosed
        frm.Show()
    End Sub

    Private Sub OnChildClosed(sender As Object, e As FormClosedEventArgs)
        Dim f = DirectCast(sender, Form)
        RemoveHandler f.FormClosed, AddressOf OnChildClosed
        If Application.OpenForms.Count = 0 Then
            ExitThread()
        End If
    End Sub
End Class

Module Program
    Public AppCtx As AppContext

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

How to use: call Program.AppCtx.ShowForm(New Form2()) from anywhere (the context can decide which windows to keep open). Notes: store the context in a public module so forms can access it; remove event handlers to avoid leaks; create explicit instances with New FormX() rather than relying on VB’s default form instances to prevent accidental shutdowns. If you prefer not to use Sub Main, the Project Properties → Application settings must be adjusted (disable the VB application framework to use a Sub Main startup).

Recommended Answers

All 3 Replies

Hi,

Add another MainForm, And make it MDIForm by setting the property (ISMSI=True). Create Menus to show different forms. and In MenuClick, Show the Forms (In Modal)..

Regards
Veena

Or you can make sure you have your application setting set to shutdown the application only after all forms close. My guess is that it's set to form1 right now.

Another option is you can give-

form1.hide()

in the form load event of form2.

Then is closing event of form2 you can give-

me.Close()
form1.Close()
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.