Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to Create ContextMenuStrip programatically

Posted on 2013-05-13
2
Medium Priority
?
1,201 Views
Last Modified: 2013-05-14
Hi,

I've created an Excel 2007 Addin using Visual Studio 2010 (C#). In it I've managed to attach an Event to a right click over a worksheet to run my function menuShow (see below). In this function I want to create a single ContextMenuStrip, with a single button on it called Show. This button will activate a particular Range for the user (the address of which is in 'parameters'. I have the code I've written listed below, but when I step over the popup.Show code, nothing happens, No menu, no nothing. There are no Forms or Controls in play here. I just want the menu to popup over the Excel Spreadsheet. What am I doing wrong here

public static void menuShow(object[] parameters)
        {

            System.Windows.Forms.ContextMenuStrip popup = new System.Windows.Forms.ContextMenuStrip();
            popup.Enabled = true;
            System.Windows.Forms.ToolStripButton btnShow = new System.Windows.Forms.ToolStripButton();
            btnShow.Name = "Show";
            btnShow.Click += new EventHandler(showRange);
            popup.Items.Add(btnShow);
            popup.Show(System.Windows.Forms.Cursor.Position);

        }

        public static void showRange(object sender, EventArgs e)
        {
            Debug.Print("NOW WHAT");
        }
0
Comment
Question by:jonathan_hills
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 39164229
0
 

Author Comment

by:jonathan_hills
ID: 39165030
I got it working.... the final code is pasted below

        public static void menuShow(object[] parameters)
        {

            const string menuName = "mnuShow";
            CommandBar popup = null;

            //Delete Previous Menu if it exists
            foreach (CommandBar menu in excelApp.CommandBars)
                if (menu.Name.Equals(menuName))
                {
                    popup = menu;
                    break;
                }
            if (popup != null)
                popup.Delete();

            //Create the Menu
            popup = excelApp.CommandBars.Add(Name: menuName, Position: MsoBarPosition.msoBarPopup, MenuBar: false, Temporary: true);
            CommandBarButton btnShow = (CommandBarButton)popup.Controls.Add(MsoControlType.msoControlButton);
            btnShow.Caption = "Show";
            btnShow.Parameter = (string)parameters[0];
            btnShow.Click += new _CommandBarButtonEvents_ClickEventHandler(showRange);

            //Show the Menu
            popup.ShowPopup(Cursor.Position.X, Cursor.Position.Y);

            //Cleanup
            popup.Delete();

        }
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question