Solved

How to Create ContextMenuStrip programatically

Posted on 2013-05-13
2
1,110 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
2 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

777 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