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
Post a Comment