Object specific context menu using .NET

By Virupaksha Aithal

Use “AddObjectContextMenuExtension” API passing the type of the class and the content menu to attach a menu to an entity type. Refer below code sample.

[CommandMethod("ContextMenuExtTest")]
static public void ContextMenuExtTest() 
{
    ContextMenuExtension contectMenu = 
                                new ContextMenuExtension();
 
    MenuItem item0 = new MenuItem("Line context menu");
    contectMenu.MenuItems.Add(item0);
    MenuItem Item1 = new MenuItem("Test1");
    Item1.Click += new EventHandler(Test1_Click);
    item0.MenuItems.Add(Item1);
    MenuItem Item2 = new MenuItem("Test2");
    Item2.Click += new EventHandler(Test2_Click);
    item0.MenuItems.Add(Item2);
 
    //for custom entity, replace the "Line" with .NET 
    //(managed) wrapper of custom entity
    Application.AddObjectContextMenuExtension(
                Line.GetClass(typeof(Line)), contectMenu);
 
}
static void Test1_Click(object sender, EventArgs e)
{
    Application.ShowAlertDialog("Test1 clickedn");
}
 
static void Test2_Click(object sender, EventArgs e)
{
    Application.ShowAlertDialog("Test2 clickedn");
}

Comments

4 responses to “Object specific context menu using .NET”

  1. Hi,
    is it possible to ad an context menu to an object identified by an EED not with typeof()?
    Many thanks for your help.
    Jürgen

  2. Hi Jurgen,
    Yes, i think it possible. Please take a look at my new post http://adndevblog.typepad.com/autocad/2012/06/enabledisable-object-context-menu-for-specific-entity.html
    Thanks
    Viru

  3. Hi,
    thanks for your answer.
    I think you meann this or not?
    “for custom entity, replace the “Line” with .NET (managed) wrapper of custom entity”
    Do you have an example for that?
    Thanks Jürgen

  4. The statement
    ContextMenuExtension contectMenu = _
    new ContextMenuExtension();
    creates an error
    Type ‘ContextMenuExtension’ is not defined.
    I am importing the ‘Autodesk.AutoCAD.Windows’ Namespace which I have imported in my class.
    Am I missing something?

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading