如何避免在c#中打开多个窗体 [英] How to avoid multiple windows forms opening in c#

查看:79
本文介绍了如何避免在c#中打开多个窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打开了表单,我将转到另一种形式创建数据并再次传递给已经打开的表单...所以rt现在多种形式即将到来,......如何避免......是否有可能传递数据在Windows应用程序中没有显示对话框c#....

解决方案

在.NET中显示表单的方法只有两种:Show和ShowDialog。

如果你出现了多个副本,那么问题就不在于使用 - 这是你构建新实例而不是每次使用相同的实例。



创建一个私有类级变量来保存表单实例:

  private  MyForm myForm; 

不要为它赋值。

当你想显示或更新数据时,检查值:

  if (myForm ==  null 
{
myForm = new MyForm();
myForm.FormClosing + = new FormClosingEventHandler(myForm_FormClosing);
}
...将数据传递给表单属性
myForm.Show();

并在表单关闭时清除变量:

  void  myForm_FormClosing( object  sender,FormClosingEventArgs e)
{
...收集您的数据来自表单属性如果必要
myForm = null ;
}


最好避免使用多个表格。你可以只有一个主表单,永远不会创建更多的表单(我不计算几个模态表单,像对话框一样使用)。您现在拥有的表单可以是面板或标签页(使用 TabControl 。这样,从一个表单转换到另一个表单只是隐藏一个面板并显示另一个一。使用标签页,它甚至更简单:用户选择当前所需的标签,一次一个。考虑改变你的设计这种方式;它将涵盖许多情况。



同一个东西的更高级变体是可停靠的UI,就像在Visual Studio中一样。



-SA


这对我来说很有魅力:

  private   void  OpenAForm_Click( object  sender,EventArgs e)
{
bool found = false ;
尝试
{
的类=code-keyword>( int i = 0 ; i< Application .OpenForms.Count; i ++)
{
// 检查窗口是否已经打开,并且如果它是
表格n = Application.OpenForms [i];
if (n.Name == formName
{
n.BringToFront();
found = true ;
}
}
如果(!找到)
{
YourForm aForm = new YourForm();
aForm.Name = formName;
aForm.Show();
}
}
catch
{
}
}


I have form open and i am going to another form creating data and again passing to the already open form...So rt now mulitple forms are coming,....How to avoid..Is it possible to pass data without show dialog in windows application c#....

解决方案

There are only two ways to display a form in .NET: Show and ShowDialog.
If you have multiple copies appearing then it is not the use of either that is the problem - it is that you are constructing new instances instead of using the same instance each time.

Create a private class level variable to hold your form instance:

private MyForm myForm;

Do not assign it a value.
When you want to show or update the data, check the value:

if (myForm == null)
    {
    myForm = new MyForm();
    myForm.FormClosing += new FormClosingEventHandler(myForm_FormClosing);
    }
...pass the data to your form properties
myForm.Show();

And clear the variable when the form is closed:

void myForm_FormClosing(object sender, FormClosingEventArgs e)
    {
    ...collect your data from the form properties if necessary
    myForm = null;
    }


It would be the best to avoid using multiple forms at all. You can have just one main form and never create more forms (I don't count few modal forms, used like dialogs). What you have as forms now could be turned panels or tab pages (using TabControl. This way, transitions from one "form" to another would be just hiding one panel and showing another one. With tab pages, it's even simpler: the user select the tabs needed at the moment, one at a time. Think about changing of your design this way; it will cover many cases.

A more advanced variant of the same thing would be the dockable UI, pretty much like in Visual Studio.

—SA


This works likea charm for me:

private void OpenAForm_Click(object sender, EventArgs e)
        {
            bool found = false;
            try
            {
                for (int i=0;i<Application.OpenForms.Count;i++)
                {
                    //Checks if the window is already open, and brings it to the front if it is
                    Form n = Application.OpenForms[i];
                    if (n.Name == "formName")
                    {
                        n.BringToFront();
                        found = true;
                    }
                }
                if (!found)
                {
                    YourForm aForm = new YourForm ();
                    aForm.Name = "formName";
                    aForm.Show();
                }
            }
            catch
            {
            }
        }


这篇关于如何避免在c#中打开多个窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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