Select to view content in your preferred language

How to create a sub menu in the ArcObjects?

3306
2
Jump to solution
07-16-2014 04:06 AM
SanajyJadhav
Deactivated User

Hello,

I'm using C#.net and ArcObjects 10.2.1.

I need to create a custom menu that will have my custom commands and some sub menus inside it, all this dynamically. I could create a menu using IMenuDef however stuck on creating a nested ( sub) menu to hold my custom commands. I have attached the screen shot one typical sub-menu for clarification.

I guess have to use IMenuItem or some interface like that but I'm not sure. I tried to follow the example at the URL : arcobjects - How to create dynamic sub-menu in ArcMap? - Geographic Information Systems Stack Exchan...‌ but could not really understand it.

So, any help will be appreciated on this issue.

Cheers,

S.

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Hi Sanjay,

There is an ArcObjects Engine sample for creating sub menus: Creating toolbar menus that work with the ToolbarControl

But if you are trying to accomplish this for ArcMap:

  1. Create a new project as Class Library (ArcMap) in Extending ArcObjects template. The sample below is named ArcMapSubMenu.
  2. You may delete the class.cs file from Solution Explorer.
  3. Right click on the project name in Solution Explorer and select Add -> New Item. Create an ArcGIS Class from Extending ArcObjects template. In this following sample, its named as Navigation.cs.
  4. In the ArcGIS Add Class Wizard:

          a. Base Implementation:

              In Customization Group, select All and Application Framework from the dropdown menus.

              In Base Component, select Root Level Menu

          b. Component Category:

              Check ArcMap Command Bar, and click Finish.

  5. Below is for IMenuDef implementation in Navigation.cs:

    #region "IMenuDef Implementations"

        public string Caption

        {  get { return "Navigation";  }

        public void GetItemInfo(int pos, IItemDef itemDef)

        {

            //Commands for the menu - the object browser lists these commands

            switch (pos)

            {

                case 0:

                    itemDef.ID = "esriControls.ControlsMapZoomInFixedCommand";

                    break;

                case 1:

                    itemDef.ID = "esriControls.ControlsMapZoomOutFixedCommand";

                    break;           

                case 2:  // Add custom sub menu

                    itemDef.ID = "ArcMapSubMenu.ToolbarSubMenu";

                    itemDef.Group = true;

                    break;

            }

        }

        public int ItemCount

        {  get { return 3;  }

        public string Name

      {  get { return "Navigation"; }  }

        #endregion

In the above code, case 2 adds the sub menu. The ID can be GUID/ProgID for sub menu which will be created in steps 6-8.

6. Create another ArcGIS Class as in steps 3, and name it ToolbarSubMenu.cs.

7.  In the ArcGIS Add Class Wizard:

a. Base Implementation:

    In Customization Group, select All and Application Framework from the dropdown menus.

    In Base Component, select Menu this time to create a submenu.

b. Component Category:

  Check ArcMap Command Bar, and click Finish.

8. Below is the implementation of IMenuDef in ToolbarSubMenu.cs:

#region "IMenuDef Implementations"

        public string Caption

      { get { return "SubMenu"; } }

        public void GetItemInfo(int pos, IItemDef itemDef)

        {

            //Commands for the menu - the object browser lists these commands

            switch (pos)

            {

                case 0:

                    itemDef.ID = "esriControls.ControlsMapUpCommand";

                    break;

                case 1:

                    itemDef.ID = "esriControls.ControlsMapDownCommand";

                    break;

                case 2:

                    itemDef.ID = "esriControls.ControlsMapLeftCommand";

                    break;

                case 3:

                    itemDef.ID = "esriControls.ControlsMapRightCommand";

                    break;

                case 4:

                    itemDef.ID = "esriControls.ControlsMapPageUpCommand";

                    itemDef.Group = true;

                    break;

                case 5:

                    itemDef.ID = "esriControls.ControlsMapPageDownCommand";

                    break;

                case 6:

                    itemDef.ID = "esriControls.ControlsMapPageLeftCommand";

                    break;

                case 7:

                    itemDef.ID = "esriControls.ControlsMapPageRightCommand";

                    break;

            }

        }

        public int ItemCount

        { get { return 8; } }

        public string Name

        { get { return "SubMenu"; } }

        #endregion

9. Build and run the code. This menu should be available in ArcMap -> Customize Menu -> Customize Mode -> Commands tab -> Categories area -> [Menus]. In the Commands area, you will find Navigation menu. Drag it into the ArcMap GUI in the menu bar area.

Hope this helps! Thank you,

- Mohini

View solution in original post

2 Replies
by Anonymous User
Not applicable

Hi Sanjay,

There is an ArcObjects Engine sample for creating sub menus: Creating toolbar menus that work with the ToolbarControl

But if you are trying to accomplish this for ArcMap:

  1. Create a new project as Class Library (ArcMap) in Extending ArcObjects template. The sample below is named ArcMapSubMenu.
  2. You may delete the class.cs file from Solution Explorer.
  3. Right click on the project name in Solution Explorer and select Add -> New Item. Create an ArcGIS Class from Extending ArcObjects template. In this following sample, its named as Navigation.cs.
  4. In the ArcGIS Add Class Wizard:

          a. Base Implementation:

              In Customization Group, select All and Application Framework from the dropdown menus.

              In Base Component, select Root Level Menu

          b. Component Category:

              Check ArcMap Command Bar, and click Finish.

  5. Below is for IMenuDef implementation in Navigation.cs:

    #region "IMenuDef Implementations"

        public string Caption

        {  get { return "Navigation";  }

        public void GetItemInfo(int pos, IItemDef itemDef)

        {

            //Commands for the menu - the object browser lists these commands

            switch (pos)

            {

                case 0:

                    itemDef.ID = "esriControls.ControlsMapZoomInFixedCommand";

                    break;

                case 1:

                    itemDef.ID = "esriControls.ControlsMapZoomOutFixedCommand";

                    break;           

                case 2:  // Add custom sub menu

                    itemDef.ID = "ArcMapSubMenu.ToolbarSubMenu";

                    itemDef.Group = true;

                    break;

            }

        }

        public int ItemCount

        {  get { return 3;  }

        public string Name

      {  get { return "Navigation"; }  }

        #endregion

In the above code, case 2 adds the sub menu. The ID can be GUID/ProgID for sub menu which will be created in steps 6-8.

6. Create another ArcGIS Class as in steps 3, and name it ToolbarSubMenu.cs.

7.  In the ArcGIS Add Class Wizard:

a. Base Implementation:

    In Customization Group, select All and Application Framework from the dropdown menus.

    In Base Component, select Menu this time to create a submenu.

b. Component Category:

  Check ArcMap Command Bar, and click Finish.

8. Below is the implementation of IMenuDef in ToolbarSubMenu.cs:

#region "IMenuDef Implementations"

        public string Caption

      { get { return "SubMenu"; } }

        public void GetItemInfo(int pos, IItemDef itemDef)

        {

            //Commands for the menu - the object browser lists these commands

            switch (pos)

            {

                case 0:

                    itemDef.ID = "esriControls.ControlsMapUpCommand";

                    break;

                case 1:

                    itemDef.ID = "esriControls.ControlsMapDownCommand";

                    break;

                case 2:

                    itemDef.ID = "esriControls.ControlsMapLeftCommand";

                    break;

                case 3:

                    itemDef.ID = "esriControls.ControlsMapRightCommand";

                    break;

                case 4:

                    itemDef.ID = "esriControls.ControlsMapPageUpCommand";

                    itemDef.Group = true;

                    break;

                case 5:

                    itemDef.ID = "esriControls.ControlsMapPageDownCommand";

                    break;

                case 6:

                    itemDef.ID = "esriControls.ControlsMapPageLeftCommand";

                    break;

                case 7:

                    itemDef.ID = "esriControls.ControlsMapPageRightCommand";

                    break;

            }

        }

        public int ItemCount

        { get { return 8; } }

        public string Name

        { get { return "SubMenu"; } }

        #endregion

9. Build and run the code. This menu should be available in ArcMap -> Customize Menu -> Customize Mode -> Commands tab -> Categories area -> [Menus]. In the Commands area, you will find Navigation menu. Drag it into the ArcMap GUI in the menu bar area.

Hope this helps! Thank you,

- Mohini

SanajyJadhav
Deactivated User

Thanks Mohini for your help. Appreciate it. It helped a lot.

0 Kudos