如何编写代码以及在哪里可以编写,如果Windows窗体已经打开,则会再次打开Windows窗体 [英] How To Write Code And Where Can I Write, Restricting Windows Form From Opening Again If Its Already Open

查看:56
本文介绍了如何编写代码以及在哪里可以编写,如果Windows窗体已经打开,则会再次打开Windows窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写代码以及我可以在哪里编写,如果Windows窗体已经打开则限制Windows窗体再次打开



thanx

解决方案

如果你的意思是整个应用程序应该只运行一个实例,并且如果你试图打开一个实例,它在第一个关闭之前不起作用,那么这就是我使用的方法:确保只有一个实例正在运行的简单方法。 [ ^ ]



如果您的意思是使用MyForm.Show()在应用程序中打开一个窗口,并且只想要一个实例表单显示然后它有点复杂,但不多。

1)创建一个类级变量来保存新表单的实例:

  pr ivate  MyForm myForm =  null ; 



2)当您想要打开表单时,请检查它:

  if (myForm!=  null 
{
// 已经打开
.. 。
}
其他
{
...
}



3)如果它已经打开,那么只需致电

 myForm.SetFocus(); 



4)如果不是,请创建一个新实例,将其保存在 myForm 中,并在显示之前为FormClosed事件添加一个处理程序:

 myForm =  new  MyForm(); 
myForm.FormClosed + = new EventHandler(myForm_Closed);
myForm.Show();



5)在FormClosed处理程序中,清除变量:

  private   void  myForm_Closed( object  sender ,EventArgs e)
{
myForm = null ;
}





全部完成!


由于OriginalGriff弹出了软木塞瓶子在这里,我将要忙一段时间,我要添加我的作品:



1.如果您的用户界面设计 允许用户在 意图时创建某些内容他们应该无法创建内容:我称之为用户体验设计中的错误。



这导致了一个简单的想法:当用户无法与之交互时,禁用或隐藏任何控件。



2.如果你必须有一个单实例Win Application,我更喜欢使用在System中使用Mutex对象的非常标准的技术。线程:

 使用系统; 
使用 System.Threading;
使用 System.Windows.Forms;

命名空间 YourWinFormProject
{
static class 程序
{
/// < 摘要 >
/// 应用程序的主要入口点。
/// < / summary >
[STAThread]
static void Main()
{
使用(Mutex互斥锁= Mutex( false aMutex))
{
if ( !mutex.WaitOne( 0 true ))
{
MessageBox.Show ( 此应用程序仅限于一个实例。 <跨度class =code-string>错误,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
Application.Run( new Form1());
}
}
}
}
}

3。如果您继续使用错误让用户尝试创建超过一定数量的表单实例...而不是主表单...您可以使用此:< pre lang =cs> 使用 System.Linq;
使用 System.Windows.Forms;

命名空间 YourWinFormProject
{
public partial class AnotherForm:Form
{
public AnotherForm()
{
InitializeComponent();
AnotherFormInstance = this ;
}

public static AnotherForm AnotherFormInstance;

public static bool DontRecurse = false ;

受保护 覆盖 void CreateHandle()
{
if (DontRecurse)
{
DontRecurse = ;
return ;
}

if (Application.OpenForms.OfType< AnotherForm>()。Any())
{
DontRecurse = true ;
MessageBox.Show( string .Format( 您只能创建一个AnotherForm实例), 错误,MessageBoxButtons。好的,MessageBoxIcon.Error);
return ;
}

base .CreateHandle();
}
}
}

使用'DontRecurse布尔标志变量的原因是为了防止显示MessageBox两次,这是'CreateHandle'的副作用工作。



注意:在CP和StackOverFlow上没有找到任何类似的想法/代码后,我自己想出了'CreateHandle'的技巧;然而,这没什么大不了的;我确信那里有一个例子。如果您知道不应该使用此技术的任何原因,请告诉我。


how to write code and where can i write, Restricting Windows Form from opening again if its already open

thanx

解决方案

If you mean the whole application should only run one instance, and if you try to open a second it doesn't work until the first is closed, then this is the method I use: A simple way to ensure only one instance is running.[^]

If you mean that you open a window within your application using MyForm.Show() and only ever want one instance of the form to show then it's a little more complex, but not much.
1) Create a class level variable to hold the instance of your new form:

private MyForm myForm = null;


2) When you want to open the form, check it:

if (myForm != null)
   {
   // already open
   ...
   }
else
   {
   ...
   }


3) If it's already open, then just call

myForm.SetFocus();


4) If it isn't, create a new instance, save it in myForm and add a handler for the FormClosed event before you show it:

myForm = new MyForm();
myForm.FormClosed += new EventHandler(myForm_Closed);
myForm.Show();


5) In the FormClosed handler, clear the variable:

private void myForm_Closed(object sender, EventArgs e)
    {
    myForm = null;
    }



All done!


Since OriginalGriff "popped the cork on the bottle" here, and I'm going to be busy for a while, I am going to add my piece:

1. If your user-interface design allows a user to create something when it is your intent they should not be able to create something: I call that a mistake in user-experience design.

This leads to the simple idea: disable or hide any Control when it makes no sense for the user to interact with it.

2. If you must have a single-instance Win Application, I prefer to use the very standard technique that uses the Mutex object in System.Threading:

using System;
using System.Threading;
using System.Windows.Forms;

namespace YourWinFormProject
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            using (Mutex mutex = new Mutex(false, "aMutex"))
            {
                if (!mutex.WaitOne(0, true))
                {
                    MessageBox.Show("This Application is limited to one Instance.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1());
                }
            }
        }
    }
}

3. If you are going ahead with the mistake of letting the user attempt to create more than a certain number of instances of a Form ... not the "Main Form" ... you can use this:

using System.Linq;
using System.Windows.Forms;

namespace YourWinFormProject
{
    public partial class AnotherForm : Form
    {
        public AnotherForm()
        {
            InitializeComponent();
            AnotherFormInstance = this;
        }

        public static AnotherForm AnotherFormInstance;

        public static bool DontRecurse = false;

        protected override void CreateHandle()
        {
            if (DontRecurse)
            {
                DontRecurse = false;
                return;
            }

            if (Application.OpenForms.OfType<AnotherForm>().Any())
            {
                DontRecurse = true;
                MessageBox.Show(string.Format("You can only create 1 instance of AnotherForm"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            base.CreateHandle();
        }
    }
}

The reason for using the 'DontRecurse boolean flag variable is to prevent showing the MessageBox twice which is a side-effect of the way 'CreateHandle "works."

Note: the technique of over-riding 'CreateHandle is something I came up with myself after not finding any similar idea/code on CP and StackOverFlow; however, that's no big deal; I am sure there's an example out there somewhere. If you know of any reason why this technique should not be used, please let me know.


这篇关于如何编写代码以及在哪里可以编写,如果Windows窗体已经打开,则会再次打开Windows窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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