Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. adding an item to context menu

adding an item to context menu

Scheduled Pinned Locked Moved C#
helptutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Muhammad Ashraf Nadeem
    wrote on last edited by
    #1

    dear fellows i hope u will all be fine it is my first question on any forum. so i do not know how to put a question and how to get an answer.although i have read the instructions of how to get the answer yet i m not sure.. so at least one person answer my question. i want to add a menu item which should display in the menu when user right click on a file and some when user will point to this menu item. some other options to be shown. how to do this.pl help me (by sending code) or some reference material or help topic. waiting for your kind response A.Nadeem

    L 1 Reply Last reply
    0
    • M Muhammad Ashraf Nadeem

      dear fellows i hope u will all be fine it is my first question on any forum. so i do not know how to put a question and how to get an answer.although i have read the instructions of how to get the answer yet i m not sure.. so at least one person answer my question. i want to add a menu item which should display in the menu when user right click on a file and some when user will point to this menu item. some other options to be shown. how to do this.pl help me (by sending code) or some reference material or help topic. waiting for your kind response A.Nadeem

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, I'm not sure what your question is. If you are creating a program (in C# as implied by this forum) then you want to use the ContextMenu.Popup event to clear, then populate your context menu with items that depend on the item for which context is requested. Code would include things such as:

      // once, probably in constructor
      contextMenu.Popup+=new System.EventHandler(myPopup);

      // event handler
      private void myPopup(object sender, EventArgs e) {
      Menu menu=sender as Menu;
      menu.MenuItems.Clear();
      mi=addMenuItem(menu, "Copy File", new EventHandler(CopyFileHandler));
      mi.Enabled=LongNameIsFileSpec;
      mi=addMenuItem(menu, "Explore", new EventHandler(ExploreHandler));
      mi.Enabled=LongNameIsFileSpec;
      ...
      }

      protected MenuItem addMenuItem(Menu menu, string text, EventHandler handler) {
      return addMenuItem(menu, text, Shortcut.None, handler);
      }

      protected MenuItem addMenuItem(Menu menu, string text, Shortcut sc,
      EventHandler handler) {
      MenuItem menuItem=new MenuItem(text);
      menuItem.Click+=handler;
      menuItem.Shortcut=sc;
      menu.MenuItems.Add(menuItem);
      menuItem.Enabled=handler!=null;
      return menuItem;
      }

      If you are referring to the context menu presented by Windows Explorer, there are basically two ways to add user defined menu items: - you can add an item for all files, all folders, or all files+folders by creating the right entries in the System's Registry - you can have Explorer start a small user-provided program to dynamically add one or more context menu items very time something gets right clicked. Hope this helps. :)

      Luc Pattyn [My Articles]

      M 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, I'm not sure what your question is. If you are creating a program (in C# as implied by this forum) then you want to use the ContextMenu.Popup event to clear, then populate your context menu with items that depend on the item for which context is requested. Code would include things such as:

        // once, probably in constructor
        contextMenu.Popup+=new System.EventHandler(myPopup);

        // event handler
        private void myPopup(object sender, EventArgs e) {
        Menu menu=sender as Menu;
        menu.MenuItems.Clear();
        mi=addMenuItem(menu, "Copy File", new EventHandler(CopyFileHandler));
        mi.Enabled=LongNameIsFileSpec;
        mi=addMenuItem(menu, "Explore", new EventHandler(ExploreHandler));
        mi.Enabled=LongNameIsFileSpec;
        ...
        }

        protected MenuItem addMenuItem(Menu menu, string text, EventHandler handler) {
        return addMenuItem(menu, text, Shortcut.None, handler);
        }

        protected MenuItem addMenuItem(Menu menu, string text, Shortcut sc,
        EventHandler handler) {
        MenuItem menuItem=new MenuItem(text);
        menuItem.Click+=handler;
        menuItem.Shortcut=sc;
        menu.MenuItems.Add(menuItem);
        menuItem.Enabled=handler!=null;
        return menuItem;
        }

        If you are referring to the context menu presented by Windows Explorer, there are basically two ways to add user defined menu items: - you can add an item for all files, all folders, or all files+folders by creating the right entries in the System's Registry - you can have Explorer start a small user-provided program to dynamically add one or more context menu items very time something gets right clicked. Hope this helps. :)

        Luc Pattyn [My Articles]

        M Offline
        M Offline
        Muhammad Ashraf Nadeem
        wrote on last edited by
        #3

        Hi i was referring to context menu presented by windows explorer and i want to show it against all files and folders. so how to write enteries to registery... what enteries and where(in the registery)... waiting for your reply. thanks in advance

        J L 2 Replies Last reply
        0
        • M Muhammad Ashraf Nadeem

          Hi i was referring to context menu presented by windows explorer and i want to show it against all files and folders. so how to write enteries to registery... what enteries and where(in the registery)... waiting for your reply. thanks in advance

          J Offline
          J Offline
          jjansen
          wrote on last edited by
          #4

          You need to use Shell-extensions for that. Try a search on the interfaces IContextMenu, IContextMenu2 and IContextMenu3.

          1 Reply Last reply
          0
          • M Muhammad Ashraf Nadeem

            Hi i was referring to context menu presented by windows explorer and i want to show it against all files and folders. so how to write enteries to registery... what enteries and where(in the registery)... waiting for your reply. thanks in advance

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi, this is how I do it: - I have my program contain a menu item to install itself as a possible tool to open all files and folders - when that menu item is clicked, it creates two registry entries: HKEY_CLASSES_ROOT\AllFilesystemObjects\Shell\myProgName.ContextMenu (Default)=text to show in context menu HKEY_CLASSES_ROOT\AllFilesystemObjects\Shell\myProgName.ContextMenu\Command (Default)=myProgPath %1 myProgPath can be obtained as Environment.GetCommandLineArgs()[0] %1 means append the file/folder selected, and will result in a command line arg For manipulating the registry, I use P/Invoke to OpenKey, CloseKey, and RegSetValueEx since I want my code to also work under .NET 1.x; but starting with 2.0 there are easier ways to do it. BTW the (Default) key is accessed with name=null. :)

            Luc Pattyn [My Articles]

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups