Select to view content in your preferred language

Add commandItems to existing context menu (Feature Class Context Menu in ArcCatalog)

5229
8
09-02-2010 03:35 AM
Udaya_BhaskerCheerala
Emerging Contributor
Hi All,

I would like to add Items to an existing Feature Class context menu in ArcCatalog through C#.NET. could any one please provide me examples how to do this task.

Thanks in advance.
Uday
0 Kudos
8 Replies
MarkWelsh
Emerging Contributor
Here is the way I do it to add some ICommands that I have in an extension, to a context menu in ArcMap.  This is being done with Visual Basic, but the concept should translate to C#

First, you need to identify either the name, or guid of the context menu you want from ArcCatalog.  The ArcObjects help should help you there.  In my code it looks like this:

Dim myCmdBar as ICommandBar

Set myUID = New esriSystem.UID
myUID.Value = "esriArcMapUI.MapViewContextMenu"

Then you need to get the ICommandBar that is the target context menu in ArcCatalog...

Set myCmdBar = pApp.Document.CommandBars.Find(myUID)  ' keep in mind I'm doing this in ArcMap...

Then you can set about adding items to this command bar:

Set myMenUID = New esriSystem.UID  ' this will reference an ICommand from my extension
myMenUID.Value = "keySheet_Extension.cKSConflictFindComm"  ' my ICommand I want to add

Dim myCmdItem As esriframework.ICommandItem
Set myCmdItem = myCmdBar.Find(myMenUID) 

' check if it is already present on the context menu...

If myCmdItem Is Nothing Then

     Set myCmdItem = myCmdBar.Add(myMenUID, myCmdBar.Count - 1)
      myCmdItem.Caption = "Find next graphic conflict"
       myCmdItem.Group = True
       myCmdItem.Refresh

End If

... repeat for each item you want to add to the context menu

Hope this helps!

Mark
0 Kudos
Udaya_BhaskerCheerala
Emerging Contributor
Hi Mark,

Thank you very much for the quick reply. I am trying to add command item to Feature Layer Context Menu in ArcMap with the below code. But I am not able to add. could you please help me what's wrong with the below code.



private void AddCommandItem()
        {
            IMxDocument m_MxDoc = m_application.Document as IMxDocument;
            IDocument ThisDoc = m_MxDoc as IDocument;
            ICommandBars CommandBars = ThisDoc.CommandBars as ICommandBars;
            UID CustomCommandUID = new UID();
            CustomCommandUID.Value = "ContextMenu.cmdMyItem";                  
            object Missing = Type.Missing;
            // add command to feature layer context menu
            UID MenuUID = new UID();
            MenuUID.Value = "esriArcMapUI.FeatureLayerContextMenu";
        
            ICommandBar FeatureLayerContextMenu = CommandBars.Find(MenuUID.Value, false, false) as ICommandBar;
            ICommandItem CustomCommandItem = FeatureLayerContextMenu.Find(CustomCommandUID, false);
          
            if (!(CustomCommandItem != null))
                FeatureLayerContextMenu.Add(CustomCommandUID, ref Missing);
            // add command to raster layer context menu
            MenuUID.Value = "esriArcMapUI.RasterLayerContextMenu";
    
            ICommandBar RasterLayerContextMenu = CommandBars.Find(MenuUID, false, false) as ICommandBar;
            CustomCommandItem = RasterLayerContextMenu.Find(CustomCommandUID, false);
            // if not already there, add it
            if (!(CustomCommandItem != null))
                RasterLayerContextMenu.Add(CustomCommandUID, ref Missing);
            // add command to group layer context menu
            MenuUID.Value = "esriArcMapUI.GroupLayerContextMenu";
           
            ICommandBar GroupLayerContextMenu = CommandBars.Find(MenuUID, false, false) as ICommandBar;
            CustomCommandItem = GroupLayerContextMenu.Find(CustomCommandUID, false);
            // if not already there, add it
            if (!(CustomCommandItem != null))
                GroupLayerContextMenu.Add(CustomCommandUID, ref Missing);
        }






Thanks,
Uday
0 Kudos
JohnHauck
Frequent Contributor
Do you have a requirement to only do this with code? If not you can simply do the following:

- open the Customize dialog, under the Toolbars tab select Context Menus
- click the Context Menus drop down and scroll to "Feature Layer Context Menu"
- you can click and drag commands or tools from the commands tab directly to the desired menu
0 Kudos
Udaya_BhaskerCheerala
Emerging Contributor
I need to do it through program.

Thanks
Uday



Do you have a requirement to only do this with code? If not you can simply do the following:

- open the Customize dialog, under the Toolbars tab select Context Menus
- click the Context Menus drop down and scroll to "Feature Layer Context Menu"
- you can click and drag commands or tools from the commands tab directly to the desired menu
0 Kudos
JoeVondracek
Regular Contributor

            ICommandBar FeatureLayerContextMenu = CommandBars.Find(MenuUID.Value, false, false) as ICommandBar;


I think that the first parameter to the Find method should be just the UID object, like
            ICommandBar FeatureLayerContextMenu = CommandBars.Find(MenuUID, false, false) as ICommandBar;


For some reason, when you are trying to get a handle to the feature layer context menu, you are using the value of the UID in the Find method.  All of your other calls to the Find method use the UID itself.
0 Kudos
Udaya_BhaskerCheerala
Emerging Contributor
Hi,
even though I pass only MenuUID Iam not able to add the item to context menu.

Thanks,
Uday






I think that the first parameter to the Find method should be just the UID object, like
            ICommandBar FeatureLayerContextMenu = CommandBars.Find(MenuUID, false, false) as ICommandBar;


For some reason, when you are trying to get a handle to the feature layer context menu, you are using the value of the UID in the Find method.  All of your other calls to the Find method use the UID itself.
0 Kudos
JohnHauck
Frequent Contributor
What version are you working with? I can add with the following:

        private void AddCommandItem()
        {
            object Missing = Type.Missing;

            IDocument ThisDoc = m_MxDoc as IDocument;

            ICommandBars CommandBars = ThisDoc.CommandBars as ICommandBars;

            UID CustomCommandUID = new UID(){
                Value = "SomeClassLibrary.SomeCommand"
            };

            UID MenuUID = new UID(){
                Value = "esriArcMapUI.FeatureLayerContextMenu"
            };

            ICommandBar FeatureLayerContextMenu = CommandBars.Find(MenuUID, false, false) as ICommandBar;

            FeatureLayerContextMenu.Add(CustomCommandUID, ref Missing);
        }



How about if you try with a standard ArcGIS command rather than a custom one, does that work?

try:

        private void AddCommandItem()
        {
            object Missing = Type.Missing;

            IDocument ThisDoc = m_MxDoc as IDocument;

            ICommandBars CommandBars = ThisDoc.CommandBars as ICommandBars;

            UID CustomCommandUID = new UID(){
                Value = "esriArcMapUI.ZoomToSelectedCommand"
            };

            UID MenuUID = new UID(){
                Value = "esriArcMapUI.FeatureLayerContextMenu"
            };

            ICommandBar FeatureLayerContextMenu = CommandBars.Find(MenuUID, false, false) as ICommandBar;

            FeatureLayerContextMenu.Add(CustomCommandUID, ref Missing);
        }
0 Kudos
JoeVondracek
Regular Contributor
Hi,
even though I pass only MenuUID Iam not able to add the item to context menu.

Thanks,
Uday


It might be a question of WHEN you are trying to add the item to the context menu, not HOW.   If you are trying to add the item to the context menu during ArcMap startup, you need to wait until the application framework is fully initialized.  Otherwise, your framework customizations don't appear.  Check out the "Customizing At Startup" in the Developer Kit samples for how to do that.
0 Kudos