Me.Close() Question

A

Alice

Hi,

In my program, the user can navigate to many different
forms. When they go to the next form, I want the form they
have left to close.

However, the forms aren't closing. Can anyone tell me what
I'm doing wrong? Here is a sample snippet of code where
the user can return to the main form while simultaneously
closing the previous form.

Thank you!

Private Sub btnBack_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnBack.Click

' Goes Back to the main form
Dim objMain As New frmMain()

objMain.ShowDialog()

' Closes the Travel Screen
Me.Close()
End Sub
 
C

Codemonkey

The problem is that you are calling Close() *after* calling ShowDialog() on
the other form.

ShowDialog() will pause execution at that point until the form being shown
has closed.

Try using the Show() method instead of ShowDialog(), or alternatively (but
messy), call close before calling ShowDialog.


Hope this helps,

Trev.
 
A

Alice

Thanks for your response.

Unfortunately, neither method seems to be working. I have
used both, but now the program breaks.

If you do want to open another form and close the previous
one, how should it be coded? (For example, a simple splash
screen that should close as the user clicks to enter the
program?)

Many thanks,
Alice
 
B

Bernie Yaeger

Hi Alice,

I have a password form that closes when the correct pwd is entered; works
very simply but it does not directly open the next form, as it opens an mdi
container that ultimately is the base form of the mdi app.

However, opening one form from another is pretty easy. I tested the
following with 2 forms - formfirst and formsecond. This is the code from
the first form,which opens the second and closes itself.
Dim newmdichild As New formsecond

Me.Close()

newmdichild.Show()

Now the code for the second form is the same, except it calls formfirst.

This works fine with show(), but with showdialog(), it exhibits strange
behavior - each remains open and simply displays a second, third etc
instance of itself.

Can you show me your code, and the error message you get when the program
breaks?

HTH,

Bernie
 
H

Herfried K. Wagner [MVP]

* "Alice said:
In my program, the user can navigate to many different
forms. When they go to the next form, I want the form they
have left to close.

However, the forms aren't closing. Can anyone tell me what
I'm doing wrong? Here is a sample snippet of code where
the user can return to the main form while simultaneously
closing the previous form.

Thank you!

Private Sub btnBack_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnBack.Click

' Goes Back to the main form
Dim objMain As New frmMain()

objMain.ShowDialog()

' Closes the Travel Screen
Me.Close()
End Sub

Using 'ShowDialog' will block the execution until the form you showed is
closed. Then the 'Me.Close()' will be executed.

"Solution":

<http://groups.google.com/groups?selm=u#[email protected]>
 
A

Alice

Hi Bernie,

My original code (using ShowDialog) just left every form
still open, but the program didn't break. So I changed it
to .Show(), and it suddenly exits the program. Here is the
message it gives after it crashes:

The program '[3088] TravelCenter.exe' has exited with code
0 (0x0).

Here is the code I'm using:

Private Sub btnEnter_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnEnter.Click

' Open the Main Form
Dim objMain As New frmMain()

objMain.Show()

' Closes Welcome Screen
Me.Close()


End Sub

Thank you for your help!
 
B

Bernie Yaeger

Hi Alice,

You're closing the main form of the app, I suspect. This form is the base
form, so the programs exits.

What you have to do is make the main form an mdi container (in its
properties). Then develop a simple menu for that form; have one of the menu
options open a child form, which then has a button that can open a different
child form and close itself, etc. This way, the mdi container (the parent
form) remains open all the time and serves as a lauching point for any menu
option you place in it.

If you need some help setting this up, try first to do it and let me know
where you run into a problem. The menu calls should look something like
this:
Private Sub MenuItem31_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem31.Click

Dim newmdichild As New formfirst

newmdichild.MdiParent = Me

newmdichild.Show()

End Sub

Then, inside formfirst, a button click event like this to launch a different
form and close itself:

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

Dim newmdichild As New formsecond

Me.Close()

newmdichild.Show()

End Sub

HTH,

Bernie

Alice said:
Hi Bernie,

My original code (using ShowDialog) just left every form
still open, but the program didn't break. So I changed it
to .Show(), and it suddenly exits the program. Here is the
message it gives after it crashes:

The program '[3088] TravelCenter.exe' has exited with code
0 (0x0).

Here is the code I'm using:

Private Sub btnEnter_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnEnter.Click

' Open the Main Form
Dim objMain As New frmMain()

objMain.Show()

' Closes Welcome Screen
Me.Close()


End Sub

Thank you for your help!
-----Original Message-----
Hi Alice,

I have a password form that closes when the correct pwd is entered; works
very simply but it does not directly open the next form, as it opens an mdi
container that ultimately is the base form of the mdi app.

However, opening one form from another is pretty easy. I tested the
following with 2 forms - formfirst and formsecond. This is the code from
the first form,which opens the second and closes itself.
Dim newmdichild As New formsecond

Me.Close()

newmdichild.Show()

Now the code for the second form is the same, except it calls formfirst.

This works fine with show(), but with showdialog(), it exhibits strange
behavior - each remains open and simply displays a second, third etc
instance of itself.

Can you show me your code, and the error message you get when the program
breaks?

HTH,

Bernie






.
 
C

Cor

Hi Alice,

I think from what you describe the dialog form is that what you need
although there are also other ways to do it.

I made a sample for you with something extra in it to hide the control box
in the splash screen if you use that and to go back from form2 automticly
when button1 is clicked on that. It closes than automaticly.

I hope it helps?

Cor

\\\Main form
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim frm As New Form2
Me.Hide()
frm.ShowDialog()
Me.Show()
frm.dispose
End Sub
///
\\\Sub form 2
Private Sub Form2_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.CancelButton = Me.Button1
Me.ControlBox = False
End Sub
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top