Need Help with CommandBarButton event handling in Visio


A

Ashok

Hi,

I'm trying to create an new Add-In for Visio in C#. I had created a new
CommandBar and a CommandBarButton in the Visio which is shown as a new Menu
Bar with a single Menu Item. I have a button click menu handler, which opens
a FileChooser dialog and imports a file to it. The MenuBar and MenuItem are
all created in the OnStartupComplete function. The methods used are as
follows,

public void
AddMenuToMenuBar(Microsoft.Office.Interop.Visio.Application application,
string caption)
{
if (application != null)
{
try
{
applicationCommandBars =
(CommandBars)application.CommandBars;
menuBar = applicationCommandBars.ActiveMenuBar;

// Add a pop-up menu to the end of the active menu bar.
menuPopup =

(CommandBarPopup)menuBar.Controls.Add(MsoControlType.msoControlPopup, 1, "",
10, true);
menuPopup.Caption = caption;
}
catch (Exception err)
{
LogMessageToFile(err.Message);
throw;
}
}
}

public void AddMenuItemToMenuBar(CommandBarPopup menuPopup, string
menuItemCaption)
{
if (menuPopup != null)
{
try
{
menuItem =
(CommandBarButton)menuPopup.Controls.Add(MsoControlType.msoControlButton, 1,
"", 1, true);
menuItem.Caption = menuItemCaption;
menuItem.Style = MsoButtonStyle.msoButtonCaption;
menuItem.Visible = true;
menuItem.Click += new
_CommandBarButtonEvents_ClickEventHandler(MenuItem_Click);
}
catch (Exception err)
{
LogMessageToFile(err.Message);
throw;
}
}
}

And my MenuItem_Click function is as follows,

private void MenuItem_Click(CommandBarButton control, ref bool
cancelDefault)
{
try
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "All files (*.*)|*.*";
ofd.InitialDirectory = "C:\\";
ofd.Title = "Choose a File to generate Visio Diagram";
string file = (ofd.ShowDialog() == DialogResult.OK) ?
ofd.FileName : null;
LogMessageToFile("File Chosen for creating visio " + file);
if (file == null)
{
MessageBox.Show("No File Chosen");
}
else
{
GenerateVisioDiagram(file);
}
}
catch (Exception ee)
{
}
}

In the GenerateVisioDiagram, I open a new document to the existing
application,

public void GenerateVisioDiagram(string inputFile)
{;
try
{
Microsoft.Office.Interop.Visio.Documents documents =
visioApplication.Documents;


Microsoft.Office.Interop.Visio.Document currentDocument =
documents.Add("");

// Set the Name of the page
Microsoft.Office.Interop.Visio.Page currentPage =
currentDocument.Pages[1];

// Other Logic to generate a diagram in the page

}

For some reasons, the Menu Item that I created stops firing any events after
the documents.Add("") method call.

I have the commandBars, commandBar, commandBarButtom and the commandBarPopUp
all declared as class variables as follows,

CommandBarButton menuItem;
CommandBar menuBar;
CommandBars applicationCommandBars;
CommandBarPopup menuPopup;

If any of aware of the reason why the Menu Item click event is not fired
after the first time, please share it here. Any help will be greatly
appreciated.

Thanks,
Ashok.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top