Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I created a static menu contains 3 static items like in this link create menus, I want to add a sub-menu and get the menu's items from database like in this image:

I already did the left part with static items, but i couldn't add the sub-menu like in the right side. I tried to do what in this link create dynamic menu, but it's not working.

EDIT: Menu code:

public sealed class TestMenu : BaseMenu, IRootLevelMenu {
 public ReportingMenu() {
            AddItem("Reporting.Desktop.UI.Commands.ReportSuitesCommand");// commands
            AddItem("Reporting.Desktop.UI.Commands.ReportsCommand");
            BeginGroup(); //Separator
            AddItem("Reporting.Desktop.UI.Commands.AboutReportingCommand");
        }

        public override string Caption {
            get {
                //TODO: Replace bar caption
                return "Reporting";
            }
        }
        public override string Name {
            get {
                //TODO: Replace bar ID
                return "ReportingMenu";
            }
        }
    }

multi item code:

public class HowToMultiItem : IMultiItem, IMultiItemEx
    {
        private ESRI.ArcGIS.Controls.IHookHelper _mHookHelper;

        //Step 2.
        public string Caption {
            get {
                return "Layer Zoom";
            }
        }
        public string Name {
            get {
                return "CSNETSamples_HowToMultiItem";
            }
        }
        public int HelpContextID {
            get {
                return 0;
            }
        }
        public string HelpFile {
            get {
                return "";
            }
        }
        public string Message {
            get {
                return "MultiItem single message";
            }
        }

        //Step 3.
        public int OnPopup(object hook) {
            _mHookHelper = new ESRI.ArcGIS.Controls.HookHelperClass {Hook = hook};
            return _mHookHelper.FocusMap.LayerCount;
        }

        //Step 4.
        public string get_ItemCaption(int index) {
            var layer = _mHookHelper.FocusMap.Layer[index];
            return "Zoom to " + layer.Name;
        }
        public bool get_ItemEnabled(int index) {
            //Enable zoom in only when the layer is visible.
            var layer = _mHookHelper.FocusMap.Layer[index];
            return layer.Visible;
        }
        public int get_ItemBitmap(int index) {
            return 0; //Not implemented.
        }
        public bool get_ItemChecked(int index) {
            return false;
        }

        //Step 5.
        public void OnItemClick(int index) {
            //Get the layer and its spatial referenced extent.
            var layer = _mHookHelper.FocusMap.Layer[index];
            var env = layer.AreaOfInterest;
            //Zoom to the extent and refresh the view.
            var activeView = _mHookHelper.FocusMap as
                ESRI.ArcGIS.Carto.IActiveView;
            if (activeView == null) return;
            activeView.Extent = env;
            activeView.Refresh();
        }

        //Step 6–Optional interface implementation.

        public string get_ItemMessage(int index) {
            var layer = _mHookHelper.FocusMap.Layer[index];
            return "Zoom to " + layer.Name + "layer.";
        }
        public int get_ItemHelpContextID(int index) {
            return 0;
        }
        public string get_ItemHelpFile(int index) {
            return "";
        }
    }
share|improve this question
1  
Can you post your code that you have thus far? –  artwork21 Apr 21 '13 at 15:52
    
@artwork21 i posted the code in the question –  RASHOMON Apr 22 '13 at 6:39

1 Answer 1

up vote 1 down vote accepted

You should follow three process.

1. Create RootMenu with implement BaseMenu and IRootLevelMenu. This class must been visible by Framework, So it need's to register to MxCommands Component Category.

2. Create NestedMenu with implement BaseMenu. And added by RootMenu. This class doesn't need to register to Component Category.

3. Create MultiItem with implement IMultiItem and IMultiItemEx. And added by NestedMenu. This class also doesn't need to register to Component Category.

Here is the sample code of RootMenu

[Guid("4b8018b8-2afb-4bb1-b7d1-1e7ef819b8a3")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("ArcMapClassLibrary1.TestMenu")]
public sealed class TestMenu : BaseMenu, ESRI.ArcGIS.Framework.IRootLevelMenu {

    #region COM Registration Function(s)
    [ComRegisterFunction()]
    [ComVisible(false)]
    static void RegisterFunction(Type registerType) {
        // Required for ArcGIS Component Category Registrar support
        ArcGISCategoryRegistration(registerType);

        //
        // TODO: Add any COM registration code here
        //
    }

    [ComUnregisterFunction()]
    [ComVisible(false)]
    static void UnregisterFunction(Type registerType) {
        // Required for ArcGIS Component Category Registrar support
        ArcGISCategoryUnregistration(registerType);

        //
        // TODO: Add any COM unregistration code here
        //
    }

    #region ArcGIS Component Category Registrar generated code
    /// <summary>
    /// Required method for ArcGIS Component Category registration -
    /// Do not modify the contents of this method with the code editor.
    /// </summary>
    private static void ArcGISCategoryRegistration(Type registerType) {
        string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
        MxCommandBars.Register(regKey);
    }
    /// <summary>
    /// Required method for ArcGIS Component Category unregistration -
    /// Do not modify the contents of this method with the code editor.
    /// </summary>
    private static void ArcGISCategoryUnregistration(Type registerType) {
        string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
        MxCommandBars.Unregister(regKey);
    }

    #endregion
    #endregion

    public TestMenu() {

        AddItem("ArcMapClassLibrary1.NestedTestMenu");

    }

    public override string Caption {
        get {
            //TODO: Replace bar caption
            return "Reporting";
        }
    }
    public override string Name {
        get {
            //TODO: Replace bar ID
            return "ReportingMenu";
        }
    }
}

And nested menu.

[Guid("B33F73C4-EA02-4197-91E9-F857FE2FB881")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("ArcMapClassLibrary1.NestedTestMenu")]
public sealed class NestedTestMenu : BaseMenu {

    public NestedTestMenu() {

        AddItem("HowToCodeSnippets.HowToMultiItem");
    }

    public override string Caption {
        get {
            //TODO: Replace bar caption
            return "Nested Reporting";
        }
    }
    public override string Name {
        get {
            //TODO: Replace bar ID
            return "NestedReportingMenu";
        }
    }
}

And last, MutiItem. I didn't edit anything inside of code from sample. This MultiItem items appears only TOC has at least one layer.

[Guid("4e0f22f3-5619-4b75-b9b0-36ea859db97d")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("HowToCodeSnippets.HowToMultiItem")]
public class HowToMultiItem : IMultiItem, IMultiItemEx {

    private ESRI.ArcGIS.Controls.IHookHelper m_hookHelper;

    #region "IMultiItem Implementations"

    //Step 2.
    public string Caption {
        get {
            return "Layer Zoom";
        }
    }
    public string Name {
        get {
            return "CSNETSamples_HowToMultiItem";
        }
    }
    public int HelpContextID {
        get {
            return 0;
        }
    }
    public string HelpFile {
        get {
            return "";
        }
    }
    public string Message {
        get {
            return "MultiItem single message";
        }
    }

    //Step 3.
    public int OnPopup(object hook) {
        m_hookHelper = new ESRI.ArcGIS.Controls.HookHelperClass();
        m_hookHelper.Hook = hook;
        return m_hookHelper.FocusMap.LayerCount;
    }

    //Step 4.
    public string get_ItemCaption(int index) {
        ESRI.ArcGIS.Carto.ILayer layer = m_hookHelper.FocusMap.get_Layer(index);
        return "Zoom to " + layer.Name;
    }
    public bool get_ItemEnabled(int index) {
        //Enable zoom in only when the layer is visible.
        ESRI.ArcGIS.Carto.ILayer layer = m_hookHelper.FocusMap.get_Layer(index);
        return layer.Visible;
    }
    public int get_ItemBitmap(int index) {
        return 0; //Not implemented.
    }
    public bool get_ItemChecked(int index) {
        return false;
    }

    //Step 5.
    public void OnItemClick(int index) {
        //Get the layer and its spatial referenced extent.
        ESRI.ArcGIS.Carto.ILayer layer = m_hookHelper.FocusMap.get_Layer(index);
        ESRI.ArcGIS.Geometry.IEnvelope env = layer.AreaOfInterest;
        //Zoom to the extent and refresh the view.
        ESRI.ArcGIS.Carto.IActiveView activeView = m_hookHelper.FocusMap as
            ESRI.ArcGIS.Carto.IActiveView;
        activeView.Extent = env;
        activeView.Refresh();
    }
    #endregion

    //Step 6–Optional interface implementation.
    #region "IMultiItemEx Implementations"
    public string get_ItemMessage(int index) {
        ESRI.ArcGIS.Carto.ILayer layer = m_hookHelper.FocusMap.get_Layer(index);
        return "Zoom to " + layer.Name + "layer.";
    }
    public int get_ItemHelpContextID(int index) {
        return 0;
    }
    public string get_ItemHelpFile(int index) {
        return "";
    }
    #endregion
}

Make sure that you need to change ProgID and GUID. Hope this helps you.

share|improve this answer
    
Thanks a lot, but how to implement items inside IMultiItem, can you till me please. –  RASHOMON Apr 22 '13 at 14:30
    
@lbrahim Swaiss I'm not exactly understand your question. What items do you want to implement sub item 1,2,3? –  Darksanta Apr 22 '13 at 15:44
    
never mind, i found the answer. and thanks a lot. :) –  RASHOMON Apr 22 '13 at 16:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.