creating an activeX component at runtime

G

Guest

Hi,

I have a C# application (VS2005) with Microsoft Mappoint activeX control on
a form. At a certain moment I want to create a second one temporary in code.
This seems not to work, when I try to access it I have an InvalidActiveXState
Exception.

I cannot find mutch on the web about this Invalid State of a component
except that it is invalid (what the Exception already describe). So I hope
someone here knows ?

It cannot be difficult to create an ActiveX component at runtime ? Are there
special things to do with it except calling the constructor with New ?
 
N

Nicholas Paldino [.NET/C# MVP]

Wilfried,

I'm not sure exactly what you have to do, but, if I was facing this, I
would look at the area in the designer code and see how it is instantiating
the control, and see if there is a difference in the way that you are doing
it, versus how the designer is doing it.

Hope this helps.
 
A

Andreas Mueller

Wilfried said:
Hi,

I have a C# application (VS2005) with Microsoft Mappoint activeX control on
a form. At a certain moment I want to create a second one temporary in code.
This seems not to work, when I try to access it I have an InvalidActiveXState
Exception.

I cannot find mutch on the web about this Invalid State of a component
except that it is invalid (what the Exception already describe). So I hope
someone here knows ?

It cannot be difficult to create an ActiveX component at runtime ? Are there
special things to do with it except calling the constructor with New ?

Hi,

from my experience this can come from one or both of two things.

A lot of ActiveX controls require a call to BeginInit before you access
its properties:

AxHost activeX = ...;
activeX.BeginInit()
// do something
activeX.EndInit();

The second thing is that some ActiveX controls need to have a parent to
work correct. (After all they are ActiveX controls).

AxHost activeX = ...;
activeX.BeginInit()

// parent the control
Controls.Add(activeX);

activeX.EndInit();

This however will make the control visible.

HTH,
Andy
 
G

Guest

Hi,

Thank you. It was the Controls.Add() that did it. I set Visible to false and
it is ok too. I use it in a using block to make suer it is disposed.
 
A

Andreas Mueller

Wilfried said:
Hi,

Thank you. It was the Controls.Add() that did it. I set Visible to false and
it is ok too. I use it in a using block to make suer it is disposed.
Hi,

if you are using .NET 1.1: We have found a memory leak when we dispose
ActiveX controls. Depending on the size and type of your ActiveX and how
often you create/dispose it, you might want to do something about it.

The ActiveX hooks the VisibleChanged event of the main form when added
to another control and does not unhook it on Dispose(). This means that
the ActiveX will not be garbage collected as long as the main form
lives. If your ActiveX releases all resources on dispose properly, you
only will only notice a increase on the GC Handles.

One solution would be to keep he control creates and invisible. Another
one is to unhook the event using reflection.

This issue seems to be fixed in .NET 2.0.


Cheers,
Andy
 

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