Jump to content
Xtreme .Net Talk

Closing 1 form from another


Recommended Posts

Guest density
Posted

I have a program with 3 forms: frmStartup, frmPlayers, and frmGame. frmStartup is the startup form for the application. frmPlayers is launched from frmStartup and frmGame is launched from frmPlayers. I would like to close frmGame from frmStartup (which is not the form that launched it).

 

For what its worth, this is how I'm launching frmGame from frmStartup:

 

dim frmGame as frmGame

 

frmGame = new frmGame()

frmGame.show

 

While I'm probably on the wrong path, here's how I've tried closing frmGame from frmStartup:

 

dim frmGame as new frmGame

frmGame.close (also tried frmGame.dispose)

 

This doesn't generate an error, however it doesn't close the frmGame that is already open. I'm guessing it is closing a new instance of frmGame that I created in the dim statement but never showed.

 

I have also tried:

 

dim frmGame as frmGame

frmGame.close

 

This generates an Object reference not set to an instance of the object error.

 

I also tried putting a public sub in frmGame which contains me.close and calling that sub from frmStartup. Depending on how i dim'd frmGame, it reacts the same way as the code above.

 

Any help would be greatly appreciated.

Guest Perl
Posted

The correct way to do the action you are wanting to do is to use the .dispose function, the problem is in your variable scope. You need to have 'frmGame' as a global scope if you plan on closing it outside the form its self, or you can do this inside the form by using 'Call Dispose' but remember reloding the resource takes some resource and processor time, so "most" of the time its better off using 'frmGame.Visible = False' and killing all the processes @ the end.

 

Hope this helps :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...