Solved

How to Create ContextMenuStrip programatically

Posted on 2013-05-13
2
1,142 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 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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.
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 demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

733 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