Marc Intes
Marc Intes

Reputation: 737

VB.net opening and closing forms

I have a VB program that has two forms, i have coded the form load of each forms.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    MessageBox.Show("I AM FORM 1")
End Sub

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    MessageBox.Show("I AM FORM 2")
End Sub

Here is how i switch through Form1 and Form2, i made use of a button.

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

    Me.Hide()
    Form1.Show()

End Sub

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

    Me.Hide()
    Form2.Show()

End Sub

But everytime i switch forms the form load event will only trigger once. Is there something wrong with my code? I am guesing the Me.Hide() will only hide the previous form and not totally close it. I want to be able to close the previous form so that when i will open it again, the form load event will trigger again.

Upvotes: 3

Views: 105068

Answers (6)

Jade Mendoza
Jade Mendoza

Reputation: 1

If MessageBox.Show("Are you sure to close this application?", "Close", 
    MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
    frmIndex.Show() //the main form 
Else
    e.Cancel = True
    Me.Show() // The form open
End If

The form open going closing and going back to the main/index form. hope it help :) just play with the .show, .hide and e.cancel

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 942247

I am guesing the Me.Hide() will only hide the previous form and not totally close it

Yes, it does what it says. If you want to close the form then use Me.Close() instead. The Load event will fire again when you create the new instance.

You'll have to change a setting to ensure that doesn't also close your application. Project + Properties, Application tab, change the Shutdown mode setting to "When last form closes". And put the Me.Close() call after the Show() call.

Upvotes: 4

John Bush
John Bush

Reputation: 1

Yes. What you are doing is closing the form before it could open up form2.

Instead Of:

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

    Me.Hide()
    Form1.Show()

End Sub

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

    Me.Hide()
    Form2.Show()

End Sub

You Need To Put:

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

Form1.show

Me.hide 

End Sub

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

Form2.show

Me.hide   

End Sub

If This Helps, Please Reply.

Upvotes: -1

Cody Gray
Cody Gray

Reputation: 244981

But everytime i switch forms the form load event will only trigger once. Is there something wrong with my code? I am guesing the Me.Hide() will only hide the previous form and not totally close it.

This is exactly what is happening. The Hide method just hides the form from the user, effectively making it invisible.

What you're looking for is the Close method, which actually closes the form. (Since you are displaying the form using the Show method, you do not need to call Dispose.)

You will, however, not be able to close a form and continue to run code in its methods. So you'll need to reverse the order of the statements in your event handler functions, displaying the other form first and then closing itself. Make them look like this:

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

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

That will do what you want. The Load event will be triggered each time you call the Show method, because you're creating and showing a new form.

It is worth pointing out, though, that you're relying on an unusual characteristic of VB.NET, one that it retains from the older VB languages for backwards compatibility reasons. Instead of referring to an object of your form class (like you would have to do with all other class objects), you are referring to it by the type name (the name of the class itself). You really shouldn't do that, it causes all sorts of headaches and will confuse people reading your code. It is better to just instantiate a new form object, like this:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim frm As New Form1  ' create a new Form1 object
    frm.Show()            ' ... and display it

    Me.Close()
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim frm As New Form1  ' create a new Form2 object
    frm.Show()            ' ... and display it

    Me.Close()
End Sub

When you run this code, you will likely run immediately into another problem: the first time you close Form1, your entire application will quit. This is because, by default for a new project, Form1 is designated as the "Startup form" in your project's properties ("My Project" in the Solution Explorer). You will either have to:

  • create a third form to use as the "main" form, and set the "Startup form" to this third form, or
  • change the "Shutdown mode" (also in "My Project") from "When startup form closes" to "When last form closes".

Upvotes: 9

Manny265
Manny265

Reputation: 1709

I also had a similar question. When u .Hide() you are just storing it away in memory somewhere such that when it is re-opened it doesnt have to make a new form just recalls the one from memory hence that method is not called again. You have to destroy the form. So what you can do when navigating to another form is go to that form first and then destroy the current form like so
Form2.Show()
Me.Close(). Look at my question and my accepted answer. If that works please dont forget to tick this as your accepted answer.
When my form is hidden and reloaded from another form it is not executing the code in the Load event

Upvotes: 0

Dennis
Dennis

Reputation: 1547

I think you using a silly construction, but you should;

     Private Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
          Form1.close()
     End Sub

Use the Shown event.

And use ShowDialog()

    Form1.ShowDialog()

Upvotes: -1

Related Questions