Tuesday, July 14, 2009

In C# and Visual Studio .NET 2005, how do you call one form with another?

I am making a game, and whenever the game ends I need a form to pop up asking the user if he or she would like to play the game again or exit. Is there a way to implement this using MessageBox.Show or do I have to find a way of doing it with my own form?

In C# and Visual Studio .NET 2005, how do you call one form with another?
You can do this with the MessageBox.Show() method. This method takes an overload where you can specifiy buttons and the question string to ask like so:





MessageBox.Show("Do you want play another game?", MessageBoxButtons.OKCancel);





The cool thing about this is it will wait for your user to decide what they want and you can collect their response in a special DialogResult variable.





So just do this:





DialogResult result = MessageBox.Show("Do you want play another game?", MessageBoxButtons.OKCancel);





then, just query your result and take your action as necessary like so:





if( result == DialogResult.Ok)


{


//play game


}else


{


//quit application


}





Using the MessageBox is effective and simple and should give you the functionality you require.





Good luck!





-Ralph


No comments:

Post a Comment