Closing 1 form from another

  • Thread starter Thread starter density
  • Start date Start date
D

density

Guest
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.
 
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 :)
 
Back
Top