什么是关闭表单并再次打开相同表单的方法 [英] What is the way to close the form and open the same form again
问题描述
我试图关闭它并使用此代码再次打开它回合它没有关闭我在后台找到它的形式并为它打开另一个
i try to close the from and open it again with this code bout it didn't close the form i found it in the background and open another one for it
private void Graph_Load(object sender, EventArgs e)
{
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval = 60000;//1 minutes
timer1.Tick += new System.EventHandler(Timer1_Tick);
timer1.Start();
}
private void Timer1_Tick(object sender, EventArgs e)
{
//do whatever you want
RefreshMyForm();
}
private void RefreshMyForm()
{
this.Close();
Graph1 graph = new Graph1();
graph.Show();
}
我的尝试:
What I have tried:
private void RefreshMyForm()
{
this.Hide();
var Graph1 = new Graph1();
Graph1.Closed += (s, args) => this.Close();
Graph1.Show();
}
和
and
private void RefreshMyForm()
{
this.Hide();
Graph1 sistema = new Graph1();
sistema.ShowDialog();
this.Close();
}
推荐答案
从中调用UI时需要使用 InvokeRequired 另一个线程,请参见此处的示例:
c# - 自动调用InvokeRequired代码模式 - Stack Overflow [ ^ ]
线程安全调用 [ ^ ]
如何:创建Thread-S afe调用Windows窗体控件| Microsoft Docs [ ^ ]
You need to use InvokeRequired when calling the UI from another thread, see examples here:
c# - Automating the InvokeRequired code pattern - Stack Overflow[^]
Thread-Safe Calls Using Windows Form Controls in C#[^]
How to: Make Thread-Safe Calls to Windows Forms Controls | Microsoft Docs[^]
打开另一个表单的原因是因为你正在初始化一个新的实例具有
The reason it's opening another form is because you're initializing a new instance of the form by having
Graph1 Sistema = new Graphh1();
尝试以下
Try the following
private void RefreshMyForm()
{
if (this.Visible)
{
this.Hide();
}
else
{
this.Show();
}
}
通过不初始化一个新实例,你正在使用一个实例你已经有了。它实际上并没有关闭表格,而是在一分钟之后使其不可见,并在一分钟之后使其再次可见。
我想用表格上的计时器关闭并打开表单会有点困难。你必须完成Rick发布的链接。
By not initializing a new instance you're working with the one you already have. It doesn't actually closes the form but rather makes it invisible after a minute and after another minute makes it visible again.
I think to close and open the form with the timer you have on the form would be a bit difficult. You'll have to work through the links that Rick posted.
这篇关于什么是关闭表单并再次打开相同表单的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!