I clicked submit too soon: What I said applies equally to the locally scoped variable m1, as well...
Main Topics
Browse All TopicsI'm using Visual Studio for office to create an Excel Addin. I've created a sample menu, with 2 methods. If I select a menu item, it displays my dialog box. After I close it my menu will not display again. It won't drop down when clicked on. Do you know why?
If you click choice1 or choice2, then you are done, can't do it twice. Code below:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
#region VSTO generated code
this.Application = (Excel.Application)Microso
#endregion
CheckIfMenuBarExists();
AddMenuBar();
}
// Create the menu, if it does not exist.
private void AddMenuBar()
{
try
{
Office.CommandBarButton menuCommand;
Office.CommandBarPopup cmdBarControl = null;
Office.CommandBar menubar = (Office.CommandBar)Applica
int controlCount = menubar.Controls.Count;
string menuCaption = "&Stockwatch Quotes";
// Add the menu.
cmdBarControl = (Office.CommandBarPopup)me
Office.MsoControlType.msoC
if (cmdBarControl != null)
{
cmdBarControl.Caption = menuCaption;
Office.CommandBarButton m1, m2;
// Add the menu command.
m1 = (Office.CommandBarButton)c
Office.MsoControlType.msoC
m1.Caption = "&Choice 1...";
m1.Tag = "choice1";
m1.FaceId = 65;
m1.Click += new Microsoft.Office.Core._Com
choice1_Click);
m2 = (Office.CommandBarButton)c
Office.MsoControlType.msoC
m2.Caption = "&Choice 2...";
m2.Tag = "choice2";
m2.FaceId = 65;
m2.Click += new Microsoft.Office.Core._Com
choice2_Click);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message)
}
}
private void choice1_Click(Microsoft.Of
{
MessageBox.Show("Choice 1");
CancelDefault = false;
}
private void choice2_Click(Microsoft.Of
{
MessageBox.Show("Choice 2");
CancelDefault = false;
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: symplicityPosted on 2007-09-23 at 22:54:05ID: 19946707
m2 is local in scope. After the click event is called the first time m2 goes out of scope. The solution is to declare m2 as a class level variable.