Why does this not work? What am I doing wrong?

C

Connectcase

Hi there,

and greetings from the Netherlands.

Trying to switch over from VB to VB.NET and struggling with something
that seemed very simple:
============================
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Form2.Show()
Me.Close()
End Sub

End Class
=============================
Looks rather simple: by clicking on button1, I want Form2 to show and
Form1 to close. However, nothing happened, just a quick flash and the
app exited. Someone told me to use ShowDialog instead of Show. This
worked, but now Me.Close() does not work. How the **** do I close
Form1??

Any help appreciated (this is driving me nuts!)

Thanks

Cees
 
G

Guest

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

End Class
=============================
Looks rather simple: by clicking on button1, I want Form2 to show and
Form1 to close. However, nothing happened, just a quick flash and the
app exited. Someone told me to use ShowDialog instead of Show. This
worked, but now Me.Close() does not work. How the **** do I close
Form1??

What is happening is:

1. Click Handler Fires
2. Show displays form (the flicker)
3. Close executes in quick succession, shutting down the app (or form).

What you want to do is use ShowDialog. ShowDialog stops execution until
the dialog is returned. In your case this is ideal - in other situation,
not so much (i.e. if you have background processes that need to continue
even while a form is displayed).

Try swapping show with showdialog and see how it works.
 
G

Guest

Cees,

I am assuming that you are using VB 2005.

Right-click the solution in Solution Explorer and choose Properties.

Change the Shutdown Mode to "When last form closes".

Kerry Moorman
 
R

RobinS

I found this under the "Application" Tab for the Project properties,
not the Solution properties. That's what you're talking about, right?

Thanks,
Robin S.
-----------------------------
 
G

Guest

Robin,

Yes, its under the Application tab.

I got there by right-clicking WindowsApplication1 in the Solution Explorer
window and choosing Properties.

Kerry Moorman
 
R

RobinS

Got it. I have my options set to display the Solution always, even if I
only have one
project. So my solution doesn't have the same properties as you get when
you
choose the WindowsApplication1 -- that's the actual project.

Anyway, that was a handy thing to know. Thanks!
Robin S.
--------------------------------
 
C

Connectcase

Thanks guys for all the responses, but none of it worked.

I already tried ShowDialog instead of Show, but this does not close
Form1. It leaves it open.
Changing the Shutdown Mode I also tried, but no luck; Form1 is still
open.

I am beginning to wonder if it is easier for new developpers to learn
VB.NET than it is for those who already know VB6 and keep comparing
VB.NET to that.

All I wanted was a simple replacement for the "Unload Me" statement in
VB6 and I thought "Me.Close" would be that replacement, but no luck.

I can understand that Microsoft has some funny rule about a child form
still depending on a parent form and therefore not being able to close
the parent etc. etc., but what about a nice error message or warning?

When I started to dig into VB.NET , I had no idea that the following 4
lines (as simple as they are) would keep me busy for so long:

------------------------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Form2.ShowDialog()
Me.Close()
End Sub
------------------------------------------

Maybe it's time to dust off the old Delphi 7 cd roms.....

Thanks

Cees
 
C

Connectcase

Hey, I think I did it (must be the Christmas spirit or something)....

I put the ShutDown mode on "When last form closes", changed
"ShowDialog" back to "Show" and it worked!!!

So the button on Form1 clicks to:

Form2.Show()
Me.Close()

Closing Form2 with the X-button in the corner also removes the app from
memory (checked the Task Manager), that was another thing I was worried
about.

Thanks guys for the input....

Cheers,

Cees
 
C

Cor Ligthert [MVP]

Cees,

It depend very much from where you did start your program.
In VB.Net it can be in I thought about 12 different ways.

If you use the mainform method for startup. Than that class "me" is direct
the last one if you do me.close otherwise you have to use a method that
belongs to the way you start program.

Anyway.
This beneath closes the program forever in a decent way.

Environment.exit(0) 'where 0 is for normal mode as you can see in your
console box.

And to be from the Benelux is real no uniquem in these newsgroups.

Cor
 

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