Solved

How to Create ContextMenuStrip programatically

Posted on 2013-05-13
2
1,080 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
Comment Utility
0
 

Author Comment

by:jonathan_hills
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now