To open a new window in vb by clicking a button replacing the previus windw

Dani AI

Generated

If by "replace" you mean the new window should take over the app and the old one should go away, Ancient Dragon and abelingaw are right that simply hiding works, but it keeps the old form in memory. If you close the startup form, though, the app exits. Two cleaner WinForms patterns avoid that:

  1. Hand off control using a custom ApplicationContext. This lets you close Form1 and continue running Form2 without terminating the message loop.
' Program.vb
Module Program
    <STAThread>
    Sub Main()
        Application.EnableVisualStyles()
        Application.Run(New SwapContext())
    End Sub
End Module

Public Class SwapContext
    Inherits ApplicationContext

    Public Sub New()
        Dim f1 = New Form1()
        AddHandler f1.FormClosed, AddressOf OnFirstClosed
        f1.Show()
    End Sub

    Private Sub OnFirstClosed(sender As Object, e As FormClosedEventArgs)
        Dim f2 = New Form2()
        AddHandler f2.FormClosed, Sub() Me.ExitThread()
        f2.Show()
    End Sub
End Class

With this setup, when Form1 closes, Form2 becomes the active window; when Form2 closes, the app exits. No hidden forms are left behind.

  1. Swap pages instead of forms. If your goal is a wizard-like flow, keep one top-level form and switch UserControls. This keeps state and avoids multiple windows entirely.
' On the host form with a Panel named MainPanel
Private Sub NextButton_Click(sender As Object, e As EventArgs) Handles NextButton.Click
    MainPanel.Controls.Clear()
    Dim page2 = New Page2Control() ' your second "screen"
    page2.Dock = DockStyle.Fill
    MainPanel.Controls.Add(page2)
End Sub

Notes:

  • ShowDialog (as mentioned by Ancient Dragon) is modal and blocks the caller; use it when you need a result before continuing.
  • In VB6 you would Unload; in VB.NET use Close/Dispose. Hiding should be reserved for cases where you intend to show the same form again.

Recommended Answers

All 5 Replies

You can't (easily) replace it, but you can hide the current window and call tne new window's ShowDialog().

First, you have to unload or hide the current window (where you will click the button) then show the window that you want to show using it's name.

On the button which will show the next form.

VB 6:

Private Sub Command1_Click()

Me.Hide
Form2.Show()

End Sub

VB 2008:

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Me.Hide()   // this will hide the window where this code is entered
        Form2.Show() //this will display the other window, Form2 is the name,replace it with yours, you can also use Form2.ShowDialog() 

    End Sub
End Class

Haven't tried VB 2010. :-)

got it . thanks :)

just a small corection .

 Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
Dim A as Form2()     // make the new form to a variable 
Me.Hide()   // this will hide the window where this code is entered
A.Show()     //this will display the other window, Form2 is the name,replace it with yours, you can also use Form2.ShowDialog() 
End Sub
End Class

,

It seems you were looking for vb.net code, which is a different forum from vb6. :) Irrespective, it seems that your problem was solved, please mark as such, thanx.

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.