user30643
user30643

Reputation: 207

vb.net: Using a Button on a Form to open another form, doesn't work

I encounter the following scenarios when I try to use 2 forms. My workflow is as follows:

(1) Load Form1.

(2) A click on button1 on Form1 closes Form1 and opens Form2.

Solution A: If I use the following code:

Dim oForm As New Form2
oForm.ShowDialog()
Me.Close()

Then Form1 will be under Form2 (Form1 still opens).

Solution B: If I use the following code:

Dim oForm As New Form2
oForm.Show()
Me.Close()

Then Form1 closes and Form2 opens, but Form1 is not on the top layer.

I have looked through the solutions for this, most propose solution B, but for me, both solutions won't work the way I want. Can anybody tell me the reason?

Upvotes: 4

Views: 44787

Answers (6)

user10663225
user10663225

Reputation: 1

Just go to form2 and write the Form1.hide() . I tried to close form1 but it closed my whole program.

Public Class Form2
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Form1.Hide()
    End Sub
End Class

Upvotes: -1

Tarak Bhavsar
Tarak Bhavsar

Reputation: 145

Try

Dim oForm as New Form2

oForm.Show()

and on Load event of form2

Form1.Hide()

Upvotes: 4

MonauralMass371
MonauralMass371

Reputation: 11

try this:

Dim oForm As New Form2

oForm.Show()

Me.Visible = False

You would to close your first form and this close your program. If you set him on invisible, he is not closed.

Upvotes: 0

Louis van Tonder
Louis van Tonder

Reputation: 3710

I suspect your are building a log-in dialogue... if so, or something similar, try this..

Open your main form first... (Form 2), have form2 showdialog (modally) form1... this will put form1 on top of form2.

Add a property to form 1, that gets set depending on what happens there.. sucessfull login for instance.

Close form 1 from its own methods... (after successful authentication), set the property before closing.

On form2, read this property of form1, and then dispose form1, and decide what to do... if unsuccessful login, show the login form again, end app. If successful, just gracefully exit out of the method that showed form1. Your form 2 is now the only form open.

Start with Form2

Form2_load

dim f1 as new form1
f1.showdialog
if f1.someproperty = somevalue then

 ' do something here, for instance, pop the form again, if you did not get what you were lookign for...

end if

'gracefully let the function end and form2 is now the only open form..

'dispose of form1. form1's close call does not dispose it, because it was opened modally. (showdialog)

f1.dispose
f1 = nothing

in form1, depending on what you are doing, set the custom property and call me.close, this will exit the form, and run the next code in form2.

Upvotes: 0

Baumi
Baumi

Reputation: 314

Try doing it this way:

Dim oForm As New Form2()

Me.Hide()
oForm.ShowDialog()

Me.Close()

Upvotes: 0

user3758070
user3758070

Reputation: 26

Use form.bringtofront() if you want to see the opening Form in the front, I m little confused though about what you are trying to do

Upvotes: 0

Related Questions