Hi all.

I would like to know how can I code form1 to open form2 immediately when form1 loaded in Visual Basic?

eg. Form1 opens and immediately form2 opens.

This is almost like website redirection.

Thanks.

Dani AI

Generated

Short answer: yes — call the second form from Form1, but prefer deferring the actual show until Form1 has finished its initial painting. Several replies here (notably , , and ) already demonstrate instantiating and showing Form2; the extra bits below help avoid common pitfalls (flicker, reentrancy, or accidentally terminating the app).

If you want the behavior to happen "immediately" but cleanly, use the Form1.Shown event or queue the action with BeginInvoke so the UI thread finishes the current paint cycle before creating the other form. This prevents performance and focus problems that can happen when you try to pop another window during Load. A compact pattern that covers reuse and ownership looks like this:

Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    Me.BeginInvoke(Sub()
        For Each f As Form In Application.OpenForms
            If TypeOf f Is Form2 Then
                f.Activate()
                Return
            End If
        Next
        Dim frm As New Form2()
        frm.Owner = Me
        frm.StartPosition = FormStartPosition.CenterScreen
        frm.Visible = True
    End Sub)
End Sub

If your goal is a true "redirect" (open Form2 and close Form1), beware: closing the startup form can end the whole application. Either set Form2 as the startup form in Project -> Properties -> Application, or change the shutdown mode to "When last form closes" so the app keeps running when the original startup form is closed. Alternatively, call Hide on Form1 instead of Close so the message loop continues.

Troubleshooting tips: avoid creating UI from background threads (use Invoke/BeginInvoke), set Owner to keep windows grouped, and check Application.OpenForms to prevent multiple instances. These small choices make the "open on load" pattern robust in real apps.

Recommended Answers

All 5 Replies

Hi
You call and initiate the second form, from the first forms load event:

Sub Form1_Onload ( byval sender as object, byval e as system.eventargs) Handles Form1.OnLoad
    dim NewForm as new Form2
    NewForm.Show()
    '....
End Sub

Using this technique, you can even set values of controls (or add new controls,) in the second form.

dim NewForm as new form2
'assume there is a textbox txtEntry on Form2
NewForm.txtEntry.Text ="Hello I'm on Form 2" 
NewForm.Show() 'Form2 will open with the textbox populated

You can even open the second form as a modal dialog and process results.

Dim NewForm as New Form2

If NewForm.ShowDialog = DialogResult.OK then
    'Result of OK was returned
Else
    'Other Result
end if

On your second form you would probably have a button that would set the dialog result of the form to OK provided what you wanted to happen on the form happened.

simply just paste the code in the form 1 load event
form2.show
if you want to load as dialog then put this code:
form2.showdialog
and as to create new form from form 1 then

Dim Nform as new Form
Nform.show
Nform.text = "Form2: Title"
...
....

You have done.
if this is not your question then please elaborate this

hi i hope it could be help you

1.Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
2 Form2.Show()
3 Form2.WindowState = FormWindowState.Normal
4 Form2.BringToFront()
5 End Sub

by: Rossana kaw

1.Form2.Show()

:
Can you tell whether this discussion solve your quries or not?
if yes then please mark it as solve
if not then tell what you don't get it

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.