From Cory Eicher

Wednesday, February 15, 2006

How to write a layer context menu command in C#

Another technical post today, but I promise that my next post will be lighter-fare...

So, yesterday I was researching how to do something with ArcObjects, and since I'm forcing myself to eat the healthy cereal that is C#, as opposed the sugary VB.NET that I'm used to, I did everything in C#.

I was trying to figure out how to add a custom command to a standard ArcMap layer context menu (doing all of this with code of course). In other words, I wanted my command to appear when a user right clicks on a layer in the ArcMap table of contents..

This is easy to do from VBA. If you've already compiled/registered your custom C# command, you can just write a little sub like this:

Sub AddCommandToLayerContextMenus()

' custom command
Dim pCommandUID As New UID
pCommandUID.Value = "MyNamespace.MyCustomCommand"
' or use GUID
'pCommandUID.Value = "{F9B2E774-29A0-4eaa-B8E2-D26DA1F21CB2}"

' add to feature layer context menu
Dim pCmdBar As ICommandBar
Set pCmdBar = ThisDocument.CommandBars.Find _

(arcid.FeatureLayerContext_Menu)
pCmdBar.Add pCommandUID

' add to group layer context menu
Set pCmdBar = ThisDocument.CommandBars.Find _

(arcid.GroupLayer_ContextMenu)
pCmdBar.Add pCommandUID

' add to raster layer context menu
Set pCmdBar = ThisDocument.CommandBars.Find _

(arcid.RasterLayer_ContextMenu)
pCmdBar.Add pCommandUID

End Sub


But, I wanted to manage all of this from my C# code. So, I wrote a custom extension that automatically adds my command to several ArcMap layer context menus on start up.

I've documented this here: How to Add a Custom ArcObjects C# Command to a Standard ArcMap Layer Context Menu

Talk to you soon,

-Cory

www.eicher-gis.com

7 Comments:

MiniFTSE said...

Nice post, good to see some useful material on one of the many gis blogs, some of them aren't half waffle!

5:02 PM

 
Anonymous said...

This post has been removed by a blog administrator.

1:28 AM

 
Anonymous said...

This post has been removed by a blog administrator.

3:57 AM

 
Anonymous said...

This post has been removed by a blog administrator.

1:35 AM

 
Anonymous said...

This post has been removed by a blog administrator.

4:57 AM

 
Anonymous said...

This post has been removed by a blog administrator.

3:12 PM

 
Anonymous said...

This post has been removed by a blog administrator.

12:57 AM

 

Post a Comment

<< Home