c# - Method for create new instance of form -


i beginner in programming. try create small application many forms. explain, how open form creating instance of form using method.

i have this:

    private void firtstoolstripmenuitem_click(object sender, eventargs e)     {         if(myform == null)         {             myform = new myform();             myform.mdiparent = this;             myform.formclosing += myformform_formclosing;             myform.show();         }         else         {             myform.activate();         }     }      void myform_formclosing(object sender, formclosingeventargs e)     {         myform = null;     } 

i want handle many tool strip menu, , dont want wrtite code in each of them, want write method opening form type.

this allows display @ 1 mdi child form of each class:

// stores references form of each type: private dictionary<type, form> _childwindows = new dictionary<type, form>();  private form showform<t>() t : form {     var formtype = typeof(t);     // if display form of given type -> activate     if (_childwindows.containskey(formtype))     {         var form = _childwindows[formtype];         form.activate();         return form;     }     else     {         // else create new form instance         var form = (form) activator.createinstance(typeof(t));         form.mdiparent = this;         form.formclosing += myformform_formclosing;         _childwindows[formtype] = form;         form.show();         return form;     } }  void myform_formclosing(object sender, formclosingeventargs e) {     _childwindows.remove(sender.gettype()) } 

usage:

private void firtstoolstripmenuitem_click(object sender, eventargs e) {    var form = showform<myform>(); } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -