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
Solved

How to Create ContextMenuStrip programatically

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
copy same as above data 18 38
how to do this Excel search box Soundex Search 6 28
Getting rid of #VALUE! 7 23
Unprotect for adding content within a Text Box 2 29
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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

837 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