Wakka02
Wakka02

Reputation: 1541

How to open windows forms in the same window

Assuming I want to open Form B from Form A, I use the following code in form A:

FormB.Show()

Me.Close()

This results in form A's window closing and Form B's window opening. This is very disruptive, so is it possible to open form B in form A's window? I've read something about an MdiContainer, so I did the following already:

-Created a new Windows Form in Visual Studio, calling it MainForm and setting its isMdiContainer property to True. -Used the following code when opening Windows Forms:

FormB.MdiParent = MainForm
FormB.Show()

Me.Close()

But this results in FormB not showing up at all.

How do I do this?

--EDIT--

Based on the below replies, it seems the general consensus is to exclude Me.Close(). I've done that, but it seems that the problem isn't Me.Close(), but FormB.MdiParent = MainForm. For some reason, whenever I include that line in FormB's Load function, FormB doesn't even show up at all.

Upvotes: 1

Views: 10100

Answers (4)

Amr Habib
Amr Habib

Reputation: 1

You can use panel object as container to your new form.

Upvotes: 0

Learning Developer
Learning Developer

Reputation: 62

I Think You Want to show Both Forms at a Time. then for that Don't Use the .Close() Method for Form A. Well there's another thing IF you want, when Form B is Called Form A can be Hide using its .Hide() Method. also you can do the same from the child Form using the main forms object to show main form and Hide the Child Form.

Upvotes: 0

Idle_Mind
Idle_Mind

Reputation: 39152

From FormA, do something like:

FormB frmB = new FormB()
frmB.MdiParent = Me
frmB.Show()

Don't close the current form with Me.Close()!

Upvotes: 0

Creator
Creator

Reputation: 1512

So you have 2 forms , lets say "MainForm and Form1"

In youre mainForm you add the code "Me.IsMdiContainer = True" in the form load event.
To set the MdiContainer to the form.

Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.IsMdiContainer = True
End Sub

And a button to open the second form.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Form1.Show()
End Sub

Then in the form1 loadevent you add the code "Me.MdiParent = MainForm"

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.MdiParent = MainForm
End Sub

Then it works "and dont call me.close then you close the form."

Upvotes: 2

Related Questions