** No Title can explain this one ** check it out :)

Posted on 2006-03-26
Last Modified: 2010-04-16
Okay, probably a major bad on my part but I thought it would be clever to create a look up table in sql server that holds the name of the form i.e lookForm1, description Example Form, display name Example

i.e. values from the table
"lookform1", "Form Test", "This is Test form 1"
"lookform2", "Form Test 2", "This is another Test"

this was fine for a couple of forms, bound to a dropdown list to pull the id(lookform1) and displayname(Form Test) so that when a user clicks this the form displays as follows

string _form = "look" + ddlLookupTables.SelectedValue.ToString();
                              case "lookCallStatus":
                                    lookCallStatus frm = new ERP.Forms.LookupForms.lookCallStatus();
                              case "lookCallType":
                                    lookCallType frm1 = new ERP.Forms.LookupForms.lookCallType();

Now the problem, the issue is that these lookup forms have grown dramatically and I'm tired of typing this shit over and over.

is there a way to pull that id (lookform1) and actually cast it as a form so that I can just do something like this.
string _form = "look" + ddlLookupTables.SelectedValue.ToString();

bugging the hell out of me and it's my fault hoping someone has a good idea.

Question by:joshcrosby
    LVL 22

    Expert Comment


      You can do that using Activator.CreateInstance, here's a sample

                      string s = "Form2";                  

                      Form frm = (Form)Activator.CreateInstance("urAssemblyName","urAssemblyName.Form2").Unwrap();

    LVL 1

    Author Comment

    I'm sorry can you elaborate a bit, not sure what you mean
    LVL 11

    Accepted Solution

    Here is an example,

      string _form = ddlLookupTables.SelectedValue.ToString();

      Form frm = (Form)Activator.CreateInstance(Type.GetType(_form));

    to determine how to refer to the forms in the database, use this code:



      If typeof(lookCallStatus).FullName returns "WindowsApplication1.lookCallStatus" then you would call Activator.CreateInstance(Type.GetType("WindowsApplication1.lookCallStatus")) to create an instance of the lookCallStatus form.
    LVL 1

    Author Comment

    Thanks a ton, that makes sense, damn that saved a ton of time, thanks again.
    LVL 1

    Author Comment

    man you have no idea how much code and speed that changed, thanks a million.
    LVL 11

    Expert Comment

    Thanks for posting that feedback; it's very rewarding to know that helped!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now