| 发表于:2007-01-12 11:47:092楼 得分:20 |
由于在mdi中,子窗体会很多,那么在我以前一篇文章中所提到的方法就有些繁琐了。为了能适应多个子窗体,并对于每个子窗体只保留一个实例,那么我就用一个新的方法来替换,即用反射方法来作处理。 大致代码如下。 首先是通过子窗体类型名来判断是否打开新的子窗体,还是把原有的子窗体进行显示。 using system.reflection; /// <summary> /// open child window /// </summary> /// <param name= "childtypestring "> </param> private void openwindow( string childtypestring ) { form mychild = null; if( !containmdichild( childtypestring ) ) { // get current process assembly assembly assembly = assembly.getEXECutingassembly(); // create data type using type string type typform = assembly.gettype( childtypestring ); // create object using type 's "invokemember " method object obj = typform.invokemember( null, bindingflags.declaredonly ¦ bindingflags.public ¦ bindingflags.nonpublic ¦ bindingflags.instance ¦ bindingflags.createinstance, null, null, null ); // show child form if( obj != null ) { mychild = obj as form; mychild.mdiparent = this; mychild.show(); mychild.focus(); } } } /// <summary> /// search mdi child form by specific type string /// </summary> /// <param name= "childtypestring "> </param> /// <returns> </returns> private bool containmdichild( string childtypestring ) { form mymdichild = null; foreach(form f in this.mdichildren) { if( f.gettype().tostring() == childtypestring ) { // found it mymdichild = f; break; } } // show the exist form if( mymdichild != null) { mymdichild.topmost = true; mymdichild.show(); mymdichild.focus(); return true; } else return false; } 以上两部分就可以对于每个子窗体只创建一个实例。那么调用以上代码就非常简单了。 如: //open a mdi child form which type named "mdichild " openwindow( typeof( mdichild ).tostring() ); 引用自愚翁的blog | | |
|