c# - Create and Show a WinForm -
i´m trying create , show forms in vs 2013 generic method method:
void showorupdateform<t>(ref form form) t : form { if (form == null) { form = activator.createinstance(typeof(t)) t; form.show(); } else form.windowstate = formwindowstate.normal; form.focus(); }
this code got here. when try call method told that:
myform1 form; showorupdateform<myform1>(ref form);
it give me error:
error 1 best overloaded method match 'gestioneas.gestioneas.showorupdateform(ref system.windows.forms.form)' has invalid arguments
error 2 argument 1: cannot convert 'ref package.views.form' 'ref system.windows.forms.form'
how can fix this?
seems, want that:
// i'd rather return potentially created form directly (not via "ref") // need "this" in method? that's why "static" static t showorupdateform<t>(t form = null) t: form, new() { if (null == form) form = new t(); // <- no activator since "new()" constraint declared form.windowstate = formwindowstate.normal; form.bringtofront(); // <- if form not foreground 1 if (form.canfocus) // <- better check have exception form.focus(); return form; } ... // more compact , readable myform1 form = showorupdateform<myform1>();
Comments
Post a Comment