您的位置:程序门 -> .net技术 -> c#



如何防止多次加载同一子个窗口


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何防止多次加载同一子个窗口
发表于:2007-03-22 14:14:40 楼主
有两个窗口a和b  
b是a的子窗口
在父窗口a中当点击一个按钮后加载b窗口
private   a_button_click(object   o,eventargs   e)
{
          b   b   =   new   b();
          b.mdiparent   =   this;
          b.show();

}
但是这样每点点a窗口上的按钮都会新生成一个子窗口,这样有就重复的窗口显示了,我想要在创建窗口之前先判断当前是否已经有窗口了,如果有就不新建b窗口对象,而直接激活  
要求:不使用类级别的变量,或全局变量,静态变量
发表于:2007-03-22 14:20:361楼 得分:0
如何使窗体不重复被打开
                要使窗体不被重复打开,就要把已经打开的窗体保存下来进行轮寻,我们可以用arraylist来保存已经打开的窗体实例,当new新的form时,对arraylist中的object进行逐个的比较,可以对form的name进行比较,也可以对form的type进行比较(前者的效率比后者低)。虽然这个方法比较笨,但却十分有效,具体代码见下:
mainform.cs
public   static   arraylist   formlist   =   new   arraylist();
private   void   button1_click(object   sender,   system.eventargs   e)
{
          bool   onlyform   =   true;
          foreach(   form   f   in   formlist   )
          {
                  if(   f   is   myform   )
                  {
                            f.activate();
                            onlyform   =   false;
                  }
          }
          if(   onlyform   )
          {
                  myform   temp   =   new   myform   ();
                  temp.owner   =   this;
                  formlist.add(   temp   );
                  temp.show();
          }
}
myform.cs
private   void   myform_closing(object   sender,   system.componentmodel.canceleventargs   e)
{
          mainform.formlist.remove(   this   );
}
 
          首先在mainform中new一个static的formlist,然后在打开窗体时在fromlist中轮寻,找到与此类型相一致的就activate(),否则就new一个新的form,并且加到formlist中,在form退出时千万别忘记把自己从formlist中退出来,就是用remove(this)就可以了。
          以上这个方法可以使当前不同类型的窗体同时运行,但是相同类型的文件只能打开一次。要是想让相同类型的窗体也可以运行,那么就要指定一个唯一的窗体标识,再轮寻这个标识。代码如下:
private   bool   checkformactivate(string   name   )
{
          try
          {
                  foreach(   form   f   in   formlist   )
                            if(   f.name.equals(name)   )
                            {
                                    f.location   =   new   point(   0   ,   0   );
                                    f.activate();
                                    return   false;
                            }
                  return   true;
          }
          catch(   exception   ex   )
          {
                  messagebox.show(   ex.message   );
                  return   true;
          }
}
private   void   button1_click(object   sender,   system.eventargs   e)
{
          if(checkformactivate(“onlyidentification”))
          {
                  myform   temp   =   new   myform   ();
                  myform.name   =   “onlyidentification”;
                  temp.owner   =   this;
                  formlist.add(   temp   );
                  temp.show();
          }
}
其中onlyidentification为你自己设定的窗体的唯一标识字符串并且在new窗体时把此字符串赋给form的name属性。
 
          如果只是想使某一个窗体不被重复打开,那么可以使用一个static   form作唯一标识,请参见我的”如何使mdi子窗体不重复被打开”
发表于:2007-03-22 14:20:572楼 得分:0
重写窗体的formclosing事件,将关闭操作重写成hide,下一次直接show就行了
发表于:2007-03-22 14:21:363楼 得分:0
在this的mdichildren里查找有没有类名为b的子窗体就可以了
发表于:2007-03-22 14:25:164楼 得分:0
private   form   detectchildform(string   childformname)
                {
                        for   (int   index   =   this.mdichildren.length   -   1;   index   > =   0;   index--)
                                if   (childformname   ==   this.mdichildren[index].name)
                                        return   this.mdichildren[index];
                        return   null;
                }


private   a_button_click(object   o,eventargs   e)
{
        if   (null   ==   this.detectchildform( "b "))
        {
                  b   b   =   new   b();
                  b.mdiparent   =   this;
                  b.show();
        }
        else
        {
                  this.detectchildform( "b ").bringtofront();
        }
}
发表于:2007-03-22 14:27:365楼 得分:0
简单易行   比用啥list记都好
发表于:2007-03-22 14:31:406楼 得分:0
if   (this.mdichildren.length   ==   0)
{
        b   b   =   new   b();
        b.mdiparent   =   this;
        b.show();
}


快速检索

最新资讯
热门点击