Handling the Double-Click Event with the Visio API

I have already covered the Visio event model in one of the previous posts. Unfortunately, not every event can be handled in that easy way. For instance, the double-click event requires a totally different approach.

In order to handle the double-click on a shape you have to use ShapeSheet that was briefly mentioned before. The ShapeSheet contains the EventDblClick property. We can handle it if we assign the VBA code that executes QUEUEMARKEREVENT function that can pass the control back to C#. Then, we just handle the MarkerEvent in Visio.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        //don't forget to add this line in order to be able to hande the double-click event
        this.visioControl.Window.Application.MarkerEvent +=
        new Visio.EApplication_MarkerEventEventHandler(Application_MarkerEvent);
        this.visioControl.DocumentOpened +=
        new AxMicrosoft.Office.Interop.VisOcx.EVisOcx_DocumentOpenedEventHandler(visioControl_DocumentOpened);
    }

    private void Application_MarkerEvent(Visio.Application app, int SequenceNum, string ContextString)
    {
        //make sure that it is a double-click
        if (ContextString != null && ContextString.Contains("/cmd=DoubleClick"))
        {
            string sourceTag = "/source=";
            MessageBox.Show("DoubleClick at ShapeID: " + ContextString.Substring(ContextString.IndexOf(sourceTag) +
            sourceTag.Length));
        }
    }

    private void visioControl_DocumentOpened(object sender, AxMicrosoft.Office.Interop.VisOcx.EVisOcx_DocumentOpenedEvent e)
    {
        //iterate through all the shapes and assign a handler for the DoubleClick event
        foreach (Visio.Shape shape in this.visioControl.Window.Application.ActivePage.Shapes)
        {
            shape.Cells["EventDblClick"].Formula = "=QUEUEMARKEREVENT(\"/cmd=DoubleClick /source=" + shape.ID + "\")";
        }
    }

    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (openDiagramDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            this.visioControl.Src = openDiagramDialog.FileName;
        }
    }
}

As you can see when executing VBA's QUEUEMARKEREVENT() we can use an arbitrary string as its argument where we can pass any value. In the example above, we pass cmd=DoubleClick as a code for the event and ‘source= ` for passing a shape ID, so that we know which shape was double-clicked.

Project Files

References

Please, refer to these pages to get additional information on:

Mike Borozdin (Twitter)
22 November 2011

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way. My personal thoughts tend to change, hence the articles in this blog might not provide an accurate reflection of my present standpoint.

© Mike Borozdin