ConnectionsAdded Event Driving Me CRAZY

C

Craig Eddy

The ConnectionsAdded event seems to work on my development machine. It
doesn't work at all on machines with only Visio 2003, .NET Framework,
and my app installed. If I manually call my AdviseEvents method I can
sometimes get the event to fire ONCE, but only once. Here's my
AdviseEvents method:

System.Diagnostics.Trace.WriteLine( "ProjectVisioWindow.AdviseEvents
called..." );
MessageBox.Show("AdviseEvents called");

axDrawingControl1.Window.Application.ConnectionsAdded += new
Microsoft.Office.Interop.Visio.EApplication_ConnectionsAddedEventHandler(Application_ConnectionsAdded);
axDrawingControl1.Window.Application.ConnectionsDeleted += new
Microsoft.Office.Interop.Visio.EApplication_ConnectionsDeletedEventHandler(Application_ConnectionsDeleted);
axDrawingControl1.Window.Application.BeforeShapeDelete += new
Microsoft.Office.Interop.Visio.EApplication_BeforeShapeDeleteEventHandler(Application_BeforeShapeDelete);
axDrawingControl1.Document.Application.ConnectionsAdded += new
Microsoft.Office.Interop.Visio.EApplication_ConnectionsAddedEventHandler(Application_ConnectionsAdded);
axDrawingControl1.Window.Application.Documents.ConnectionsAdded +=
new Microsoft.Office.Interop.Visio.EDocuments_ConnectionsAddedEventHandler(Application_ConnectionsAdded);

In response to a menu item click, my application creates a new modal
form which contains the drawing control. I have placed calls to the
above code in as many places as I can think of. What am I missing?

I've also tried using EventList.AddAdvise to add this event, but I get
nothing but exceptions from that. I used the following code, called
against just about every EventList I could find:

connectsEventSink = new
ConnectsAddedEventSink(axDrawingControl1.Window.Application.ActivePage);

try
{
axDrawingControl1.Window.Application.EventList.AddAdvise(
(unchecked( (short) visio.VisEventCodes.visEvtAdd ) + (short)
visio.VisEventCodes.visEvtConnect),
connectsEventSink, "", "connect event");
}
catch( Exception )
{
System.Diagnostics.Trace.WriteLine(" Window.Application.EventList
didn't work ");
}


Does ANYONE have a reliable way to connect to this event or any clue
as to why it would work on my dev machine but not a target machine?
The ShapeAdded event seems to work just fine no matter where I run.

Thanks in advance,
Craig
 
C

Craig Eddy

Update:

I have one application I've written where calls to app.AddAdvise()
works perfectly. In another, more complicated application, the exact
same call to AddAdvise, using the exact same event sink class, fails
with "An exception occurred.".

I have no clue what difference between the two apps is causing this to
occur. I;ve eliminated almost all of them and still the problem
occurs.
 
C

Craig Eddy

Update #2:

I moved the windows form class that hosts the drawing control into a
new project. AddAdvise works like a champ without any modification. So
something in the project definition was causing the call to fail.
Haven't a clue what it could be.
 
C

Craig Eddy

Update #2:

I moved the windows form class that hosts the drawing control into a
new project. AddAdvise works like a champ without any modification. So
something in the project definition was causing the call to fail.
Haven't a clue what it could be.

WOw...wasn't anything I expected it to be. The forms I had the Visio
drawing control placed on were part of a larger, mature project built
as a Windows Application. AddAdvise was throwing an exception despite
the event codes being correct and the sink class being valid.

I finally moved the Visio stuff to a new class library project. That
worked great. Then I noticed that I didn't have an AssemblyInfo.cs
file in my new library. So I copied the one from my original Windows
Application project. This caused the AddAdvise to break again.

OK, at least I know it's something in the AssemblyInfo.cs file. There
were three entries I thought might be the cause:

[assembly:CLSCompliant(false)]

[assembly:FileIOPermission(SecurityAction.RequestMinimum,
Unrestricted=true)]

[assembly:ComVisible(false)]

Changing CLSCompliant to true wouldn't even allow the class library to
compile:

Argument type 'AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl' is
not CLS-compliant

Commenting out the attribute didn't fix the exception with AddAdvise.

Neither did commenting out the FileIOPermission attribute.

It turns out that the ComVisible(false) attribute was the culprit.
Removing this attribute from the file fixed the exception being raised
from AddAdvise.

I suppose that in hindsight this makes sense. Everything else in the
drawing control seemed to work just fine though.

So, the lesson: if you're going to use AddAdvise with the Visio 2003
drawing control, DO NOT compile your assembly with ComVisible(false)!

Craig
 

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