I get a "Object reference not set..." error when I try to open my application after i closed it

R

Rob

Hi everyone

This is my situation. I have a main Form where i call a Form with a
VisioDrawingControl object (I call it the visioForm) included. When I
call the visioForm for the first time it doesn't give a problem. When
i close the visioForm and then try to create a new one. I get the
following error : "object reference not set to an instanve of an
object".
When i don't create a Application object it works but when i include
it it doens't work anymore. This is very strange because the first
time i call the visioform everything works fine. Could somebody give
me a solution for my problem.

Here is my c# code.


For the calling of my visioForm
-------------------------------

private void button1_Click(object sender, System.EventArgs e) {
VisioApplication test = new VisioApplication();
test.Show();
}

The VisioForm
-------------
public class VisioApplication : System.Windows.Forms.Form {
private AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl
axDrawingControl1;
private Microsoft.Office.Interop.Visio.Application app;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public VisioApplication()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//I think the major problem is in here but i don't know where.
app = this.axDrawingControl1.Window.Application;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
 
C

Chris Roth [ Visio MVP ]

I remember reading something about the drawing control "not really being
there" until it's Visible property turns to true. I've run into this
non-consistantly with VB.NET, but haven't had a real problem. One of the C#
samples (OfficePlan) in the SDK has this code:

/// <summary>The onDrawingControlVisibleChanged method handles the

/// event that is raised when the Visio ActiveX control has been fully

/// initialized. This handler is an alternative to the form-load

/// event for initializing the ActiveX control.</summary>

/// <param name="sender">The object that raised the event. This

/// is ignored for this case.</param>

/// <param name="e">The arguments associated with the

/// event. This is ignored for this case.</param>

private void onDrawingControlVisibleChanged(object sender, System.EventArgs
e) {

// If the drawing control is not on the list of initialized

// drawing controls, the control needs to be initialized.

if ((drawingControl.Visible) &&

(! drawingControlList.Contains(drawingControl.Name))) {

// Setup the drawing, open the product stencil and start the

// event sink.

setUpVisioDrawing();

// Since all of the initialization tasks succeeded, save

// the drawing control in the list of initialized controls.

drawingControlList.Add(drawingControl.Name);

}

}

I've had so many problems with the Visio control flaking out at design time
that I always create it at run time. I made a wrapper that takes a panel
control as the host for a Visio control. That way, I can play with the panel
on the form, and at run time, I hand the panel to the VisControlWrapper,
which puts a Visio Control into the panel. Seems to work rather nicely.
--

Hope this helps,


Chris Roth
Visio MVP
 
R

Rob

I have found a kind of a soultion for my problem but maybe you have
some experience with it. It's a lot of code for something simple but
it is the only way it wants to work for now.
In my Visio form i have written a property that gets my Application
property and a property for the drawingControl. It looks like this.
CODE
----
public Microsoft.Office.Interop.Visio.Application app{
get{
return this.axDrawingControl1.Window.Application;
}
}

public AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl
DrawingControl{
get{
return this.axDrawingControl1;
}
}

After that i have created a class with some properties that gets the
drawingpad and application in my visioForm (with the getter) and
return it. And it looks like this.

CODE
----

public AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl
DrawingControl{
get{
return ((FunctionCreator)main.ActiveMdiChild).DrawingControl;
}
}

public Microsoft.Office.Interop.Visio.Application application{
get{
return((FunctionCreator)main.ActiveMdiChild).app;
}
}

I now use this getter for getting the properties of my visio Form and
it works. Maybe you see some problems that i could get in the future.
Thanx in advance
Rob Van Pamel
 

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