winforms - C# Create, check, loop through objects -


such weird situation appears in front of me. here thing. if in php or other programming language easy in c# dont know how can achieve this.

brief description of problem:

  1. i have userform in c# combobox. in combobox have many items there in database -> dynamically filled on start of application
  2. each item should represent single userform. here problem, if user choses item , clicks on button check should fired if userform has been created, if not, new (object ?) userform must created. delete , recreate new form everytime user choses new option want keep userforms , create them once , hide them or show them, textinputs etc. remain filled while application run (but dont thing creating 15-20 userforms @ start of app okay in terms of performance).

so thinking of doing , tried:

  1. i wanted use name of chosen item combobox , use object (userform) name. (also perform check if exists before creating) -> apparently not possible in c# use string variable object name

  2. i googled dictionaries, can store there whole userforms ?

  3. also thinking of creating 15-20 userforms @ beginning , looping through them , show them or hide them.

any ideas ?

this caching problem. want cache forms.

so first need container cache, e.g.

var cache = new list<userform>(); 

then, when form needed, need special method search cache , add if needed. because there more 1 type of form, you'll want use generics here:

t getorcreateform<t> t: userform, new() {     userform f = cache.oftype<t>().firstordefault();  //search cache     if (f == default(t))                              //if not found,     {         f = new t();                                  //create anew         cache.add(f);                                 //and add cache     }     return f;                                         //return form created/retrieved } 

you call method, passing type of form desired:

var form1 = getorcreateform<form1>(); form1.show(); 

the above assumes there 1 type of form (one class) each row in list. if have common form class, distinguished else (e.g. maybe each form has unique name property set @ run time) you'd need dictionary tell forms apart:

var cache = new dictionary<string, userform>();  userform getorcreateform(string name) {     userform f;     if (!cache.trygetvalue(name, out f))     {         f = new userform { name = name };         cache.add(name, f);      }     return f;          }   var myform = getorcreateform("someuniquename"); myform.show(); 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -