Weird behaviour in modal dialog with ActiveX controls


For a simple test program I was implementing, I had a couple of forms – Form 1 which had all the authentication stuff and Form 2 which had the ActiveX object. The idea was to show Form 2 once a user has successfully logged in.

I had the following code in Form1:

if (iSiteEntFrm == null)
{
    iSiteEntFrm = new Form2(axiSiteNonVisual1);
    iSiteEntFrm.ShowDialog();
}
else
{
    iSiteEntFrm.ShowDialog();
}

Basically I’ve got one instance of Form2 which I try to hide/show once it’s been instantiated. “When a form is displayed as a modal dialog box, clicking the Close button (the button with an X at the upper-right corner of the form) causes the form to be hidden”, so it seemed like this should work. In my case, the first time Form2 was loaded, it worked fine as expected and showed the following:

image

However, when I close Form2 (by clicking ‘X’), and then click again on the button on Form1 which loads Form2, this is what I get – an empty Form2 with no controls!

image

I also tried creating a new Form2 each time the ‘load’ button was clicked:

iSiteEntFrm = new Form2(axiSiteNonVisual1);
iSiteEntFrm.ShowDialog();

but I kept getting a ‘System.Windows.Forms.AxHost+InvalidActiveXStateException’ exception.

image

I’m not sure of the exact reason for either of these errors, but it appeared to be some sort of an ActiveX initialisation issue. After spending a good 4hrs trying to resolve this issue, here’s the simple solution that finally worked!

iSiteEntFrm = new Form2(axiSiteNonVisual1);
DialogResult r = iSiteEntFrm.ShowDialog();
if (r == DialogResult.Cancel)
{
    iSiteEntFrm.Dispose();
}

Checking if ‘Close’ has been selected and then releasing the resources did the trick in this case.

Advertisements
This entry was posted in General and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s