How to make AxWebControl.Navigate work!

  • Thread starter Thread starter Nathan Allan
  • Start date Start date
N

Nathan Allan

When attempting to invoke "Navigate" on an AxWebControl that I
programmatically created, I was getting an
InvalidActiveXStateException exception. After searching around for a
solution to the problem, I couldn't find one that worked reliably. I
found a solution, deduced from the fact that the control seems to work
fine when created by the designer, yet doesn't when created
programmatically. I started with a working scenario in the designer
and eliminated code until I found the solution below. I thought that
I should post this solution, though I have no insight as to why it
works.

FWebBrowser = new AxSHDocVw.AxWebBrowser();
FWebBrowser.BeginInit(); // Seems to be critical the have the
begin...end init
FWebBrowser.Parent = <container>;
FWebBrowser.EndInit();
object LTemp = null;
FWebBrowser.Navigate(FURL, ref LTemp, ref LTemp, ref LTemp, ref
LTemp);
 
FWebBrowser = new AxSHDocVw.AxWebBrowser();
FWebBrowser.BeginInit(); // Seems to be critical the have the
begin...end init
FWebBrowser.Parent = <container>;
FWebBrowser.EndInit();
object LTemp = null;
FWebBrowser.Navigate(FURL, ref LTemp, ref LTemp, ref LTemp, ref
LTemp);

I don't know that you need to set the parent, but you probably want to add
it to the Controls collection of the form. The easiest way is to simply add
it to the Windows Forms section of the Toolbox and then drop it on your form
(see my post under "Viewin HTML in Winform application" from yesterday).

If you don't do it that way, you may need to set the OcxState member, though
I don't know how you would initially set it. Again, dropping it on a form
seems to handle that for you. That may be the source of the exception you're
getting, though.

Pete
 
Back
Top