c# - Windows Forms SingletoneFormProvider and using second constructor to get some parameters -


i have mdi forms application , use singletoneformprovider because it's required have 1 instance of form in time.

i have edit forms can used creating new records or changing existing ones. problem editing of existing record.

what i've done far :

  • i have method calls static method singletoneproviderform class , fresh instance of form :

        protected void loadform<t>(toolstripitem formbutton, string buttontext, long? loadentityid = null, bool closealreadyopened = true) t : baseform {          //some code..          openform = singletonformprovider.getinstance<t>(parentfrm, closealreadyopened);          openform.loadentityid = loadentityid;          openform.mdiparent = parentfrm;          openform.dock = dockstyle.fill;          openform.show();          openform.activate();          //more code.. 

    }

  • i have 3 delcarations of singletonformprovider.getinstance<t> :

    static public t getinstance(form owner) t : form

    static public t getinstance(form owner, bool closeallbutthis) t : baseform

    static public t getinstance(form owner, bool closeallbutthis, params object[] args) t : form

  • till fresh instance of form did :

    loadform();

  • if want send id :

    loadform(id);

and here comes problem. because try populate form fields data record provided id make query :

entity = anyform.find(loadentityid.value); 

and worked fine while doing in form_load event. when move part in constructor everytime check :

if (loadentityid.hasvalue) 

it never has because seems way form instance constructed don't have access id constructor.

what did :

  • made change in loadform() method:

    if (loadentityid == null) { openform = singletonformprovider.getinstance(parentfrm, closealreadyopened); } else { openform = singletonformprovider.getinstance(parentfrm, closealreadyopened, loadentityid); }

  • made default constructor protected , remove :

    protected anyform() {}

  • made constructor takes 1 argument:

    public anyform(long? loadentityid) { long? anyformid = loadentityid.value; initializecomponent(); //all stuff needed }

so amazing me works. have big doubts way accomplish this. please review , guide me correct way of doing this. , of course, if somehow correct way please verify because i'm in doubt if it's ok put in production code.

p.s

as matthew watson mentioned form behave weird without default constructor end 1 default (no argument) constructor , 1 taking 1 argument - id need. makes worse, have duplicated code. i'm pretty sure that's not how it's done.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -