防止表单多次显示 [英] Prevent form from showing multiple times

查看:28
本文介绍了防止表单多次显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 windows 窗体 (c#) 中,我在用户单击按钮时显示一个窗体,它工作正常,用户可以看到窗体,但是如果用户再次单击同一个按钮,同一个窗体将再次打开,两个窗体正在显示.有什么办法可以防止这种情况发生,请给我任何参考,谢谢.这是我的代码....

In windows form (c#), i am showing a form when user click on button, it is working fine form is visible to user, but if user click again on the same button the same form is opening again two forms are displaying. Is there any way to prevent this, please give me any reference for this thank you. This is my code....

private void button1_Click(object sender, EventArgs e)
{
  Form2 obj = new Form2();
  obj.Show();
}

推荐答案

你很可能在做这样的事情:

You are most likely doing something like this:

void button1_OnClick(object sender, EventArgs e) {
    var newForm = new MyForm();
    newForm.Show();
}

因此,每次单击时,您都会显示表单的一个新实例.你想做这样的事情:

So you are showing a new instance of the form every time it is clicked. You want to do something like this:

MyForm _form = new MyForm();

void button1_OnClick(object sender, EventArgs e) {
    _form.Show();
}

这里您只有一个您希望显示的表单实例,并且只有 Show() 它.

Here you have just one instance of the form you wish to show, and just Show() it.

这篇关于防止表单多次显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆