C# 重置/重新加载当前表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15694176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:37:30  来源:igfitidea点击:

Reset/Reload current form

c#formsreloadreset

提问by Kehza

I've been trying to reset my current form to it's original state by closing it, and opening a new one. I want the form objects to be reset,the variables to be re-declared, the class objects to be cleared etc I've got everything working but the class being cleared, no matter what I do it won't create a new one with blank data.

我一直试图通过关闭它并打开一个新表单来将我当前的表单重置为它的原始状态。我想要重置表单对象,重新声明变量,清除类对象等等我已经让一切正常但类被清除,无论我做什么它都不会创建一个新的空白数据。

Here is my code:

这是我的代码:

if (btnRandom.Text == "Reset")
{

    SetupScreen form = new SetupScreen();
    form.Show();
    this.Dispose();

    //Create new class for form / or launch load events as normal 
    form.Mybattleship = new battleship()
    form.SetupScreen_Load(this, null);
}

I've tried many methods over the internet and none have worked.. even the overly complicated ones..

我在互联网上尝试了很多方法,但都没有奏效......即使是过于复杂的......

Oh I forgot to mention I need the new form to act as if it's just been loaded as normal, so the load events etc trigger

哦,我忘了提到我需要新表单来表现得好像它刚刚被正常加载一样,所以加载事件等会触发

采纳答案by Sayse

You would be better off making a method that you can call that will set default values for items that you can use when opening form and resetting...

您最好创建一个可以调用的方法,该方法将为您在打开表单和重置时可以使用的项目设置默认值...

public SetupScreen()
{
InitializeComponent();
SetDefaultValues();
}
private void SetDefaultValues()
{
//start  values..
}
public void ResetBtn_Click(object sender, EventArgs e)
{
SetDefaultValues();
}