Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Dynamically loadning Menu Items

Dynamically loadning Menu Items

Scheduled Pinned Locked Moved C#
helpcsharpxmltutorialquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sjpjs
    wrote on last edited by
    #1

    I am planning to have an application where a menu will be populated dynamically. Say the menu for the application is as below:

    File Test Help
    Test1
    Test2

    By selecting Test1 and Test2 menu I am intending to load a form from the DLL which inherits a base class used in the main application. This class has a Show method to show the child form which is the part of the DLL. I have difficulty passing the main form as parent form. Since I am novice in C# I am trying to follow the standard method as shown below. Is there any other way to achieve it? CLib.Class1 cls = new CLib.Class1(); //<- I don’t know how to declare this dynamically cls.Parent = this; // <- I get error at this place cls.Show(); In my show method I have code like below: frmChild frm = new frmChild(); frm.MdiParent=Parent; frm.Show(); Second thing, lets us say I maintain the Menu Option name and the DLL name in a INI file or XML file. I can populate the menu option dynamically but how I will use the DLL. Thanks in advance for your kind guidance.

    R L V 3 Replies Last reply
    0
    • S sjpjs

      I am planning to have an application where a menu will be populated dynamically. Say the menu for the application is as below:

      File Test Help
      Test1
      Test2

      By selecting Test1 and Test2 menu I am intending to load a form from the DLL which inherits a base class used in the main application. This class has a Show method to show the child form which is the part of the DLL. I have difficulty passing the main form as parent form. Since I am novice in C# I am trying to follow the standard method as shown below. Is there any other way to achieve it? CLib.Class1 cls = new CLib.Class1(); //<- I don’t know how to declare this dynamically cls.Parent = this; // <- I get error at this place cls.Show(); In my show method I have code like below: frmChild frm = new frmChild(); frm.MdiParent=Parent; frm.Show(); Second thing, lets us say I maintain the Menu Option name and the DLL name in a INI file or XML file. I can populate the menu option dynamically but how I will use the DLL. Thanks in advance for your kind guidance.

      R Offline
      R Offline
      Robert Rohde
      wrote on last edited by
      #2

      1. Answering your first question is a bit difficult because you didn't specify where the code resides in. What exactly is this? I assume you are not in the parent form... What is the error you are getting? 2. Reflection is the way to go:

      using System.Reflection;

      Assembly assembly = Assembly.LoadFile("C:\\MyDll.dll");
      MyBaseClass cls = assembly.CreateInstance("CLib.Class1");
      //do something with cls

      The LoadFile call is only needed once so you could make this on startup once and then just call CreateInstance several times.

      S 1 Reply Last reply
      0
      • R Robert Rohde

        1. Answering your first question is a bit difficult because you didn't specify where the code resides in. What exactly is this? I assume you are not in the parent form... What is the error you are getting? 2. Reflection is the way to go:

        using System.Reflection;

        Assembly assembly = Assembly.LoadFile("C:\\MyDll.dll");
        MyBaseClass cls = assembly.CreateInstance("CLib.Class1");
        //do something with cls

        The LoadFile call is only needed once so you could make this on startup once and then just call CreateInstance several times.

        S Offline
        S Offline
        sjpjs
        wrote on last edited by
        #3

        Thank you very much Robert for your quick response. It seems I could not present my case properly. Here I am trying again. I have a main application with MDI form with common menu items. The modules will be available to a user depending upon from which department s/he is from. For example we will have a table to store the user and what module is available to that user UserID Module And another table which contains the module name, and the menu text, that will be shown on the menu and corresponding DLL file name for that module. Module Menu Name DLLFile These DLL files will have a common method called show(). On the click event of the menu item, the show method basically will load the MDI Child form from the DLL and pass the MDI form reference of the main application as the parent form. With this the menu items on the child form will appear on the main menu of the MDI window. Hope I made it clear this time. Thanking you in for your kind help.

        1 Reply Last reply
        0
        • S sjpjs

          I am planning to have an application where a menu will be populated dynamically. Say the menu for the application is as below:

          File Test Help
          Test1
          Test2

          By selecting Test1 and Test2 menu I am intending to load a form from the DLL which inherits a base class used in the main application. This class has a Show method to show the child form which is the part of the DLL. I have difficulty passing the main form as parent form. Since I am novice in C# I am trying to follow the standard method as shown below. Is there any other way to achieve it? CLib.Class1 cls = new CLib.Class1(); //<- I don’t know how to declare this dynamically cls.Parent = this; // <- I get error at this place cls.Show(); In my show method I have code like below: frmChild frm = new frmChild(); frm.MdiParent=Parent; frm.Show(); Second thing, lets us say I maintain the Menu Option name and the DLL name in a INI file or XML file. I can populate the menu option dynamically but how I will use the DLL. Thanks in advance for your kind guidance.

          L Offline
          L Offline
          LongRange Shooter
          wrote on last edited by
          #4

          First you would have your parent form: public class Daddy : Windows.Forms.Form {} Now that form owns the menu and thus would handle the event for each menu selection. So then your event handler would be something like this: (WARNING: My machine with VS died last night so I'm tinkering with a very bad memory to write this!!!) // using menuitem.Text append namespace and load it as object child. MenuItem item = (MenuItem) sender; // I am not sure of the method below or format but generally.... Assembly assembly = Assembly.LoadFile(path+item.Name); IBaseChild child = (IBaseChild)assembly.LoadFile( nameSpace + item.Value ); child.MDIParent = this; child.Show(); -- modified at 17:25 Tuesday 18th April, 2006

          1 Reply Last reply
          0
          • S sjpjs

            I am planning to have an application where a menu will be populated dynamically. Say the menu for the application is as below:

            File Test Help
            Test1
            Test2

            By selecting Test1 and Test2 menu I am intending to load a form from the DLL which inherits a base class used in the main application. This class has a Show method to show the child form which is the part of the DLL. I have difficulty passing the main form as parent form. Since I am novice in C# I am trying to follow the standard method as shown below. Is there any other way to achieve it? CLib.Class1 cls = new CLib.Class1(); //<- I don’t know how to declare this dynamically cls.Parent = this; // <- I get error at this place cls.Show(); In my show method I have code like below: frmChild frm = new frmChild(); frm.MdiParent=Parent; frm.Show(); Second thing, lets us say I maintain the Menu Option name and the DLL name in a INI file or XML file. I can populate the menu option dynamically but how I will use the DLL. Thanks in advance for your kind guidance.

            V Offline
            V Offline
            vatzcar
            wrote on last edited by
            #5

            I didn't understand your requirement fully, but assuming that you're trying to load menu,froms and assmblies dynamically (all are unknown to you), can be be done this way: // assuming you load-up the assembly dynamically depending upon the menu press // i've used mod name 'cause i called the main form of the desired assembly directly and as my assembly is a dll so don't have an entry point exactly so i needed to make one entry point(virtually) public void invokeProcess (string assemblyName, string strModName) { Assembly assembly = Assembly.LoadFrom (assemblyName); // Walk through each type in the assembly foreach (Type type in assembly.GetTypes ()) { // Pick up a class if (type.IsClass == true) { // if it's not using right class skip it if (type.FullName != strModName + "." + strModClass) { continue; } // create an instance of the object object ibaseObject = Activator.CreateInstance (type); // this has been done 'cause i couldn't pass the ref. of main MDI directly frmDummy frmParent = new frmDummy(); frmParent.MdiParent = this; // Create the parameter list ('course without it we won't be able to determine our MDI parent) object[] arguments = new object [] {frmParent}; // Dynamically Invoke the Object foreach (MemberInfo mi in type.GetMember(strModEntry)) { type.InvokeMember (mi.Name.ToString(), BindingFlags.Default | BindingFlags.InvokeMethod, null, ibaseObject, arguments); } } } } hope this will help.

            S 1 Reply Last reply
            0
            • V vatzcar

              I didn't understand your requirement fully, but assuming that you're trying to load menu,froms and assmblies dynamically (all are unknown to you), can be be done this way: // assuming you load-up the assembly dynamically depending upon the menu press // i've used mod name 'cause i called the main form of the desired assembly directly and as my assembly is a dll so don't have an entry point exactly so i needed to make one entry point(virtually) public void invokeProcess (string assemblyName, string strModName) { Assembly assembly = Assembly.LoadFrom (assemblyName); // Walk through each type in the assembly foreach (Type type in assembly.GetTypes ()) { // Pick up a class if (type.IsClass == true) { // if it's not using right class skip it if (type.FullName != strModName + "." + strModClass) { continue; } // create an instance of the object object ibaseObject = Activator.CreateInstance (type); // this has been done 'cause i couldn't pass the ref. of main MDI directly frmDummy frmParent = new frmDummy(); frmParent.MdiParent = this; // Create the parameter list ('course without it we won't be able to determine our MDI parent) object[] arguments = new object [] {frmParent}; // Dynamically Invoke the Object foreach (MemberInfo mi in type.GetMember(strModEntry)) { type.InvokeMember (mi.Name.ToString(), BindingFlags.Default | BindingFlags.InvokeMethod, null, ibaseObject, arguments); } } } } hope this will help.

              S Offline
              S Offline
              sjpjs
              wrote on last edited by
              #6

              Thank you vatzcar for your time and kind help:rose:. After going through your code, I changed my strategy. Previously I was planning a show method in a class file (in DLL) will take the reference of the MDI form, after that it will show the menu form, in the DLL as child form. Now I am planning to standardise the main form name as frmMenu in all the DLL files. With this the code will be moved to the click event of the menu in the main application. The click event of the menu will call LoadDLL with corresponding assembly name. private void LoadDLL(string assemblyName) { Assembly assembly = Assembly.LoadFrom (assemblyName); // Walk through each type in the assembly foreach (Type type in assembly.GetTypes ()) { if (type.IsClass == true) { object ibaseObject = null ; if (type.Name == "frmMenu") { ibaseObject = Activator.CreateInstance(type); Form frmT = (Form)ibaseObject; frmT.MdiParent = this; frmT.Show(); } } } // for each }

              It is working fine:-D. Is it optimized code? Thank you.

              V 1 Reply Last reply
              0
              • S sjpjs

                Thank you vatzcar for your time and kind help:rose:. After going through your code, I changed my strategy. Previously I was planning a show method in a class file (in DLL) will take the reference of the MDI form, after that it will show the menu form, in the DLL as child form. Now I am planning to standardise the main form name as frmMenu in all the DLL files. With this the code will be moved to the click event of the menu in the main application. The click event of the menu will call LoadDLL with corresponding assembly name. private void LoadDLL(string assemblyName) { Assembly assembly = Assembly.LoadFrom (assemblyName); // Walk through each type in the assembly foreach (Type type in assembly.GetTypes ()) { if (type.IsClass == true) { object ibaseObject = null ; if (type.Name == "frmMenu") { ibaseObject = Activator.CreateInstance(type); Form frmT = (Form)ibaseObject; frmT.MdiParent = this; frmT.Show(); } } } // for each }

                It is working fine:-D. Is it optimized code? Thank you.

                V Offline
                V Offline
                vatzcar
                wrote on last edited by
                #7

                I'm glad that somehow i could help you :-O I think it's optimized. And don't thank me, it's the Dev community who deserve 'cause this is what they tought me through the time :)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups