开发者

Closing Several Windows While One Window Is Closed

开发者 https://www.devze.com 2023-02-03 18:27 出处:网络
I have a window form application(a game) that has a stats button. When the user press the stats button, a new window pops where he can see his stats.

I have a window form application(a game) that has a stats button. When the user press the stats button, a new window pops where he can see his stats.

Problem is : a user clicks the stats button and the stats window pops. when he presses the option button to change the game's option I have a code that c开发者_JS百科loses the game form but not the stats form. So if he forgets to close the stats form, it remains open.

How do I close the stats form from the game form?


Specify the owner of the form, with the Form.Owner property. In your case, this could either be the main window, or whichever window had the GUI element that was triggered by the user to spawn the new window. The new form will then close if its owner is closed. This relationship is cascading, so if you set form B's owner to be Form A, then form C's owner as B, C will close if you close A OR B (B will also close if you close A). If B spawns C, but C is useful independently from B, then set C's owner to be A (you can do so via form B's Owner property), and C will stay open when B is closed, but will still close when A closes.

Lastly, if you open the "main" program form using Application.Run(Form mainForm), when the window specified as the parameter is closed, the application will exit completely. So, if the executable spawns A, A spawns B,and B spawns C, when A is closed, the application closes and ALL windows are closed, regardless of ownership.


why not just open it as dialog using ShowDialog() so you force him to close it before back to the options form?
and if the game's form is the main form then it must automatically close other remaining forms, otherwise you have to declare the satasForm somewhere inside your game's form and manage to close it when ever the user closes your game's form

StatsForm  sFrm = new StatsForm  ();
public gameFrm()
{
InitializeComponent();
}

private void btnShowStateForm_Click(object sender, EventArgs e)
{
sFrm.Show(); //or call
//    sFrm.ShowDialog();
}

private void gameFrm_FormClosing(object sender, FormClosingEventArgs e)
{
sFrm.Close();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号