Show random Form
-
In a project, I have 1000 forms. guess you have a string contains a name of a random form. How can u show this form?
strFormName ="frmForm274"; ???any method like this ??? ???Form fx= GetForm (strFormName); ??? ???fx.show();???
I aim this for ToolBar Items. I have an Table at database.Table contains Form names. User Selects which buttons will be shown.then when user login to program, i read users toolbaritems, add them to toolbar.But i have to write some code to activate click event to show form. -
In a project, I have 1000 forms. guess you have a string contains a name of a random form. How can u show this form?
strFormName ="frmForm274"; ???any method like this ??? ???Form fx= GetForm (strFormName); ??? ???fx.show();???
I aim this for ToolBar Items. I have an Table at database.Table contains Form names. User Selects which buttons will be shown.then when user login to program, i read users toolbaritems, add them to toolbar.But i have to write some code to activate click event to show form.1000?!?!??! :confused: You can go with reflection:
Form form = (Form)Type.GetType("frmForm274").GetConstructor(new Type[0]).Invoke(new object[0]));
This code surely would create a new form everytime it is called. If you want to reuse the forms then:
//some field
private HashTable _formsHash = new HashTable();
public Form GetForm(strFormName){
if (!_formsHash.ContainsKey(strFormName)) {
_formsHash.Add(strFormName, Type.GetType(strFormName).GetConstructor(new Type[0]).Invoke(new object[0])));
}
return (Form)formsHash[strFormName];
}But be careful that holding 1000 forms in memory might become a resource problem :). Just out of interest: What are those 1000 forms for? And who built them?
-
1000?!?!??! :confused: You can go with reflection:
Form form = (Form)Type.GetType("frmForm274").GetConstructor(new Type[0]).Invoke(new object[0]));
This code surely would create a new form everytime it is called. If you want to reuse the forms then:
//some field
private HashTable _formsHash = new HashTable();
public Form GetForm(strFormName){
if (!_formsHash.ContainsKey(strFormName)) {
_formsHash.Add(strFormName, Type.GetType(strFormName).GetConstructor(new Type[0]).Invoke(new object[0])));
}
return (Form)formsHash[strFormName];
}But be careful that holding 1000 forms in memory might become a resource problem :). Just out of interest: What are those 1000 forms for? And who built them?
:) u get me wrong. none of the forms at cache. A solution consist of 12 seperate projects. all forms count is about 1000. there is one Main MDI Form Project, others are dll including forms. I wanna add a toolbar to mdi form. But i would like to make it customize by user. Each user can add his/her own short cut buttons to toolbar. Think i use Vs.net envoirenment with just "Standart" and "Layout" toolbar. I can add or remove buttons to each toolbar. In my project I wantto use just one toolbar. And user will choose his/her buttons. I add them to toolbar after login proses. When adding, i must add handler to all buttons. when user click a button, i have to create instance of form which belongs to that button then show.
-
:) u get me wrong. none of the forms at cache. A solution consist of 12 seperate projects. all forms count is about 1000. there is one Main MDI Form Project, others are dll including forms. I wanna add a toolbar to mdi form. But i would like to make it customize by user. Each user can add his/her own short cut buttons to toolbar. Think i use Vs.net envoirenment with just "Standart" and "Layout" toolbar. I can add or remove buttons to each toolbar. In my project I wantto use just one toolbar. And user will choose his/her buttons. I add them to toolbar after login proses. When adding, i must add handler to all buttons. when user click a button, i have to create instance of form which belongs to that button then show.
Assuming all assemblies are already loaded the first line I posted will do the job:
Form form = (Form)Type.GetType("frmForm274").GetConstructor(new Type[0]).Invoke(new object[0]));