Friday, March 8, 2013

VB.NET Fixing vb.net system.windows.forms.axhost+invalidactivexstateexception

The hell dreaded vb.net system.windows.forms.axhost+invalidactivexstateexception error is probably the plague of many converting old VB6 applications to VB.NET, especially if, like me, you're new to working with either. Finally, finally, I stumbled upon a forum snippet on www.CodeProject.com, page http://www.codeproject.com/Questions/55699/AxHost-InvalidActiveXStateException, and the guy's problem wasn't even mine, and wasn't even fixed, but one lil ole line of code he had fixed my issue, at least a few of them. Add controlname.CreateControl() in your form's designer page, before the rest of its properties are set. In my forms it was Me.controlname.CreateControl(). For whatever reason, Visual Studio 2008 saw fit to not include this in its code conversion. Almost got sucked into other forum opinions that you need special Active X licenses to get such legacy controls to work in .NET.
UPDATE: My suggestion above isn't for every such situation. On some forms I found that it was not a factor at all. Rather it was the way .Net forms are loaded from the form calling on them.
I had to switch the positioning of the first code accessing the values of any Active X elements to be after the 'Show' and/or 'Activate' commands for the form in which those elements reside.
Apparently, VB6 wasn't so concerned about certain things that VB .Net obsesses over.