none
Visio C# Inconsistent DoubleClick RRS feed

  • Question

  • Hi,

    I have embedded a Visio Drawing object in my C# application. I intend to handle a DoubleClick to open a customised WinForms dialog to edit the shape's text, but am experiencing extremely inconsistent behaviour.

    I have followed the code provided here, with the exception that I set the DoubleClick formula upon adding a shape, rather than to existing shapes.

    http://www.mikeborozdin.com/post/Handling-the-Double-Click-Event-with-the-Visio-API.aspx

    The outcome: sometimes it works, but often it doesn't. On the times when it doesn't, the breakpoint I have set on MarkerEvent never breaks. It is not a case of working for one shape and not for another. If it doesn't work, it won't work at all until I restart the application.

    The most bizarre thing is that I used the Visual Studio Debug --> Restart option about a dozen times in succession (so no change to even a letter of the source code) and it only worked about a third of the time. The fact that it sometimes works indicates to me that the code I have written is correct and that there is some other factor causing it. I don't understand! Any help would be greatly appreciated

    Thanks,

    David

    public class VisioDrawingControl : AxDrawingControl
    {
        public VisioDrawingControl()
        {
            InitializeComponent();
        }
        public void AttachEventHandlers()
        {
            this.visioControl.Window.Application.MarkerEvent += new Visio.EApplication_MarkerEventEventHandler(Application_MarkerEvent);
    	this.visioControl.Window.Application.ShapeAdded += new Visio.EApplication_MarkerEventEventHandler(Application_ShapeAdded);
        }
        public Document LoadStencil(string path)
        {
            if (System.IO.File.Exists(path))
            {
                this.Document.Application.Documents.OpenEx(path, 
    			(short)VisOpenSaveArgs.visAddHidden +
    			(short)VisOpenSaveArgs.visOpenRO +
    			(short)VisOpenSaveArgs.visOpenCopy);
    	    return this.Document.Application.Documents[this.Document.Application.Documents.Count];
            }
        }
     
        private void Application_MarkerEvent(Visio.Application app, int SequenceNum, string ContextString)
        {
            if (ContextString != null && ContextString.Contains("/cmd=DoubleClick"))
            {
                string sourceTag = "/source=";
     
                MessageBox.Show("DoubleClick at ShapeID: " + ContextString.Substring(ContextString.IndexOf(sourceTag) + sourceTag.Length));
            }
        }
     
        private void Application_ShapeAdded(Shape shape)
        {
            shape.Cells["EventDblClick"].Formula = "=QUEUEMARKEREVENT(\"/cmd=DoubleClick /source=" + shape.ID + "\")";
    	// Have tried this also, same effect
            //shape.Cells["EventDblClick"].Formula = "=RUNADDONWARGS(\"QUEUEMARKEREVENT\", \"/cmd=DoubleClick /source=" + shape.ID + "\")";
        }
    }
    public partial class MyControl : UserControl
    {
        private Document MyStencil;
        protected override void OnLoad(EventArgs e)
        {
            this.MyStencil = this.workAreaDrawingControl.LoadStencil(myStencilPath);
            this.workAreaDrawingControl.AttachEventHandlers();
        }
        // The following is a portion of the standard Component Designer Generated code
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
    	this.workAreaDrawingControl = new VisioDarwingControl(this.components);
            ((System.ComponentModel.ISupportInitialisze) this.workAreaDrawingControl)).BeginInit();
    	this.workAreaDrawingControl.Anchor = ((System.Windows.Forms.AnchorStyle) ((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) System.Windows.Forms.AnchorStyles.Right)));
    	this.workAreaDrawingControl.Enabled = true;
    	this.workAreaDrawingControl.Location = new System.Drawing.Point(0,0);
    	this.workAreaDrawingControl.Name = "workAreaDrawingControl";
    	this.workAreaDrawingControl.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("workAreaDrawingControl.OcxState")));
    	this.workAreaDrawingControl.Size = new System.Drawing.Size(670, 401);
    	this.workAreaDrawingControl.TabIndex = 21;
    	((System.ComponentModel.ISupportInitialisze) this.workAreaDrawingControl)).EndInit();
        }
        private VisioDrawingControl workAreaDrawingControl;
    }






    • Edited by oxtrot Tuesday, August 21, 2012 5:03 AM
    Monday, August 20, 2012 5:45 AM

Answers

  • Hi Chris,

    I have stripped back my solution (call this Solution1) to simplify the sections of the code that are interacting with visio. I got rid of second drawing control, used a form instead of a UserControl, and no longer used a class that inherited from AxDrawingControl. The simplified code is below. All I do as an action is to drag an element I have been using the "Start state") on to the document and double click it.

    In part I was doing this to be able to submit this, but also I wanted to see what line of code causes it to start failing, since I have had another solution that I have been treating as a visio playground in which the double click event seems to work successfully.

    The surprising outcome was that no matter how much I simplified Solution1, I couldn't get it to reliably pass. Also, no matter how much I modified the playground solution to be like Solution1, I could never get that one to fail.

    What I did notice was that as I simplified Solution1, the issue occurred less frequently. Originally it failed for the majority of times that I ran the solution. After stripping back the code, I noticed that it was happening perhaps once in five.

    I can only presume that something else within Solution1 is affecting the interactions with visio. We are using a number of third party products that are not present in the playground solution. Alternatively perhaps something to do with the size of the application for Solution1 is causing it, considering your comment about a potential timing problem.

    Due to these factors, I doubt that this will be easily reproduced. I managed to write a workaround over the weekend using the MouseUp event and iterating over the locations of all shapes on the document. While a little ugly, it does meet our needs for the moment, so we'll stick with that.

    Thanks for your time anyway

    Cheers,

    Dave

    public partial class MainForm : Form
    {
        private void Button1_click(object sender, EventArgs e)
        {
            MyForm form1 = new MyForm();
            form1.ShowDialog();
        }
    }
    public partial class MyForm : Form
    {
        private Document MyStencil;
        private event EApplication_ShapeAddedEventHandler shapeAddedHandler;
        private event EApplication_MarkerEventEventHandler markerEventHandler;
        public MyForm()
        {
            InitializeComponent();
        }
        public void AttachEventHandlers()
        {
            shapeAddedHandler = new EApplication_ShapeAddedEventHandler(this.Application_ShapeAdded);
            markerEventHandler= new EApplication_MarkerEventEventHandler (this.Application_MarkerAdded);
            this.visioControl.Window.Application.MarkerEvent += new Visio.EApplication_MarkerEventEventHandler(Application_MarkerEvent);
    	this.visioControl.Window.Application.ShapeAdded += new Visio.EApplication_MarkerEventEventHandler(Application_ShapeAdded);
        }
        protected override void OnLoad(EventArgs e)
        {
            this.MyStencil = this.axDrawingControl.Window.Application.Documents.OpenEx(path, 
    			(short)VisOpenSaveArgs.visAddHidden +
    			(short)VisOpenSaveArgs.visOpenRO +
    			(short)VisOpenSaveArgs.visOpenCopy);
            this.axDrawingControl.AttachEventHandlers();
        }
        private void Application_MarkerEvent(Visio.Application app, int SequenceNum, string ContextString)
        {
            MessageBox.Show("Marker Event fired");
        }
     
        private void Application_ShapeAdded(Shape shape)
        {
            shape.Cells["EventDblClick"].Formula = "=QUEUEMARKEREVENT(\"/cmd=DoubleClick /source=" + shape.ID + "\")";
        }
        // The following is a portion of the standard Component Designer Generated code
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
    	this.axDrawingControl = new AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl();
            ((System.ComponentModel.ISupportInitialisze) this.axDrawingControl)).BeginInit();
    	this.axDrawingControl.Anchor = ((System.Windows.Forms.AnchorStyle) ((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) System.Windows.Forms.AnchorStyles.Right)));
    	this.axDrawingControl.Enabled = true;
    	this.axDrawingControl.Location = new System.Drawing.Point(0,0);
    	this.axDrawingControl.Name = "axDrawingControl";
    	this.axDrawingControl.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axDrawingControl.OcxState")));
    	this.axDrawingControl.Size = new System.Drawing.Size(670, 401);
    	this.axDrawingControl.TabIndex = 21;
    	((System.ComponentModel.ISupportInitialisze) this.axDrawingControl)).EndInit();
        }
        private AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl axDrawingControl;
    }


    Monday, August 27, 2012 1:12 AM

All replies

  • Hi oxtrot,

    Thanks for posting in the MSDN Forum.

    Where is your visioControl.Src property? It seems that you didn't declear it. And I didn't see your create VisioDrawingControl's instance in your program. Did you create it in other place?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, August 21, 2012 2:51 AM
    Moderator
  • Hi Tom, thanks for replying

    Is it a requirement to set the Src property to something other than string.Empty? I am never working from an existing document, so I do not set this. I have been presuming that it is fine to use the default blank document that opens after initialising the control.

    I have added the code for the construction of the VisioDrawingControl to the code block above.

    I do not know whether this is relevant but I do have a second VisioDrawingControl embedded within the Winforms UserControl. The user does not directly interact with this second control. Based on actions elsewhere, a file is chosen and the Src property of that second control is set accordingly.

    What I have noticed today is the VisioDrawingControl.Window.Application.EventsList is empty on the occasions when I do not receive a MarkerEvent. When I do, it contains 97 items. I have tried to investigate why, but still am not getting any closer. Again, this is using Debug --> Restart, so it is the exact same code. I also do the same sequence of actions each time.

    Thanks,

    David


    • Edited by oxtrot Tuesday, August 21, 2012 4:55 AM
    Tuesday, August 21, 2012 4:54 AM
  • Hi oxtrot,

    I will involve some experts into your issue to see whether they can help you. There might be some time delay, thanks for you patience.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, August 22, 2012 6:14 AM
    Moderator
  • Great, thanks Tom

    Cheers,

    Dave


    • Edited by oxtrot Thursday, August 23, 2012 2:18 AM
    Thursday, August 23, 2012 1:44 AM
  • Hi Dave,

    At the Forum level please describe how to assemble, build and code the minimum, bare-bones demonstration of the failure of the double-click to fire.  Your issue sounds like a timing problem, and if so it represents a potential bug. When you build and post the minimum reproduction and the steps to get the same behavior the Forum viewers may be able to suggest a workaround. If not, please consider opening a Microsoft support incident to connect with a Microsoft support engineer who, using your repro demo will identify it and determine whether it’s a bug.  For a bug there is no charge for the support incident.


    Please remember to mark the replies as answer if they help and unmark them if they provide no help. and click "Vote as Helpful" this and other helpful posts, so other users will see your thread as useful. Best Regards, Chris Jensen

    Thursday, August 23, 2012 2:48 PM
    Moderator
  • When it has happened to me, it has usually turned out to be self inflicted (pointing at the wrong instantiated class, or the class has been GC'd).

    al


    If this answer solves your problem, please check Mark as Answered. If this answer helps, please click the Vote as Helpful button. Al Edlund Visio MVP

    Thursday, August 23, 2012 3:55 PM
  • Hi Chris,

    I have stripped back my solution (call this Solution1) to simplify the sections of the code that are interacting with visio. I got rid of second drawing control, used a form instead of a UserControl, and no longer used a class that inherited from AxDrawingControl. The simplified code is below. All I do as an action is to drag an element I have been using the "Start state") on to the document and double click it.

    In part I was doing this to be able to submit this, but also I wanted to see what line of code causes it to start failing, since I have had another solution that I have been treating as a visio playground in which the double click event seems to work successfully.

    The surprising outcome was that no matter how much I simplified Solution1, I couldn't get it to reliably pass. Also, no matter how much I modified the playground solution to be like Solution1, I could never get that one to fail.

    What I did notice was that as I simplified Solution1, the issue occurred less frequently. Originally it failed for the majority of times that I ran the solution. After stripping back the code, I noticed that it was happening perhaps once in five.

    I can only presume that something else within Solution1 is affecting the interactions with visio. We are using a number of third party products that are not present in the playground solution. Alternatively perhaps something to do with the size of the application for Solution1 is causing it, considering your comment about a potential timing problem.

    Due to these factors, I doubt that this will be easily reproduced. I managed to write a workaround over the weekend using the MouseUp event and iterating over the locations of all shapes on the document. While a little ugly, it does meet our needs for the moment, so we'll stick with that.

    Thanks for your time anyway

    Cheers,

    Dave

    public partial class MainForm : Form
    {
        private void Button1_click(object sender, EventArgs e)
        {
            MyForm form1 = new MyForm();
            form1.ShowDialog();
        }
    }
    public partial class MyForm : Form
    {
        private Document MyStencil;
        private event EApplication_ShapeAddedEventHandler shapeAddedHandler;
        private event EApplication_MarkerEventEventHandler markerEventHandler;
        public MyForm()
        {
            InitializeComponent();
        }
        public void AttachEventHandlers()
        {
            shapeAddedHandler = new EApplication_ShapeAddedEventHandler(this.Application_ShapeAdded);
            markerEventHandler= new EApplication_MarkerEventEventHandler (this.Application_MarkerAdded);
            this.visioControl.Window.Application.MarkerEvent += new Visio.EApplication_MarkerEventEventHandler(Application_MarkerEvent);
    	this.visioControl.Window.Application.ShapeAdded += new Visio.EApplication_MarkerEventEventHandler(Application_ShapeAdded);
        }
        protected override void OnLoad(EventArgs e)
        {
            this.MyStencil = this.axDrawingControl.Window.Application.Documents.OpenEx(path, 
    			(short)VisOpenSaveArgs.visAddHidden +
    			(short)VisOpenSaveArgs.visOpenRO +
    			(short)VisOpenSaveArgs.visOpenCopy);
            this.axDrawingControl.AttachEventHandlers();
        }
        private void Application_MarkerEvent(Visio.Application app, int SequenceNum, string ContextString)
        {
            MessageBox.Show("Marker Event fired");
        }
     
        private void Application_ShapeAdded(Shape shape)
        {
            shape.Cells["EventDblClick"].Formula = "=QUEUEMARKEREVENT(\"/cmd=DoubleClick /source=" + shape.ID + "\")";
        }
        // The following is a portion of the standard Component Designer Generated code
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
    	this.axDrawingControl = new AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl();
            ((System.ComponentModel.ISupportInitialisze) this.axDrawingControl)).BeginInit();
    	this.axDrawingControl.Anchor = ((System.Windows.Forms.AnchorStyle) ((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) System.Windows.Forms.AnchorStyles.Right)));
    	this.axDrawingControl.Enabled = true;
    	this.axDrawingControl.Location = new System.Drawing.Point(0,0);
    	this.axDrawingControl.Name = "axDrawingControl";
    	this.axDrawingControl.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axDrawingControl.OcxState")));
    	this.axDrawingControl.Size = new System.Drawing.Size(670, 401);
    	this.axDrawingControl.TabIndex = 21;
    	((System.ComponentModel.ISupportInitialisze) this.axDrawingControl)).EndInit();
        }
        private AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl axDrawingControl;
    }


    Monday, August 27, 2012 1:12 AM