Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / June 2005

Tip: Looking for answers? Try searching our database.

Hosting the IE browser in a UserControl

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Glass Half Full - 13 Jun 2005 21:38 GMT
I've created a very simple user control that basically hosts the IE browser.
The purpose was to abstract some of the logic of setting the content of the
browser from a string.  I have a small problem with this control.  When I
use this control with a tab control (placing my browser control on different
tabbed panels), I run into a dispose error if all panels are not "touched"
(displayed) during execution.  I've hacked around this by selecting all tabs
in the dispose of the application that contains the tab control.

What is the proper way to handle the IE brower in a UserControl, so that the
dispose on the UserControl doesn't have issues when
"base.dispose(disposing);" is called?

Thanks
"Peter Huang" [MSFT] - 14 Jun 2005 03:56 GMT
Hi

The problem seems to be related with a known issue when hosting webbrowser
in tabcontrol in a usercontrol.
Here is workaround that initialize the tabcontrol and webbrowser in runtime.
You may have a try.
private void UserControl1_Load(object sender, System.EventArgs e)
{

tabControl1.SelectedTab = tabPage2;
tabControl1.SelectedTab = tabPage1;

object oNull = null;
axWebBrowser1.Navigate("www.google.com", ref oNull, ref oNull, ref oNull,
ref oNull);
axWebBrowser2.Navigate("www.microsoft.com", ref oNull, ref oNull, ref
oNull, ref oNull);
}

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Glass Half Full - 14 Jun 2005 21:28 GMT
Thank you for your reply.  I've added a Load method to my user control and
it does seem to address the problems I'm having.  Let me provide greater
details.

I've constructed a user control that contains only an axWebBrowser control
(the browser control completely fills or covers the user control panel).
This user control contains the following methods: ClearContents, SetContents
and Navigate.  Navigate does pretty much what you've outlined below.
ClearContents navigates to "about:blank" and SetContents navigates to
about:blank and within the captured DocumentComplete event writes a content
string into the HTML document.  I'm doing a lot of dynamic HTML creation and
the user control is an abstraction that helps hide the details of pushing a
HTML string into a browser.  Mostly this works very well except when Dispose
it executed.    ... as an example here is what ClearContents looks like:

       public void ClearContent() {
           m_browserAction = BrowserAction.Clear;
           m_browserState = BrowserState.Requested;
           object n = null;
           if (this.InvokeRequired == true) {
               this.Invoke(new NavigateBrowser(m_browser.Navigate), new
object[] { "about:blank", n, n, n, n} );
               }
           else {
               m_browser.Navigate("about:blank", ref n, ref n, ref n, ref
n);
               }
           }

In my application I make heavy use of the above control, and place it on
several panels in a tab control.  In my application I had to code this loop
in my application's Dispose method:
     // run thru all the tab pages so you don't get a dispose error (which
gets thrown if the BrowerControl doesn't get it's window shown
     for (int i = 0; i < m_tcMain.TabPages.Count; i++) {
           m_tcMain.SelectedIndex = i;
           }
If I do not touch all the tabs, then in the user control's dispose method
"base.Dispose(disposing)" throws a
System.Reflection.TargetInvocationException ("Unable to get the window
handle for the 'AxWebBrowser' control.  Windowless ActiveX controls are not
supported").  Initially in the browser user control, I called ClearContent
from within the Contructor, but on your recommendation I moved this call to
the Load method.  However, when I remove the "tab touch loop in the
application, I still get the TargetInvocationException throw.  After that
exception is thrown a couple of times I get a NullReferenceException
("Object Reference not set to an instace of an object") in "main" of the
application.

I can avoid this problem by creating a static bool in the browser user
control that ensures that the Dispose method runs only once regardless of
how many instances of the control exists.  However, doing this without
understand why isn't exactly appealing.  If I had to guess I would say that
some part of the AxWebBrowser is shared across all instances of the object
and that the first object that dispose of it, leave all the other instances
broken.

Thanks

> Hi
>
[quoted text clipped - 24 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
"Peter Huang" [MSFT] - 15 Jun 2005 03:56 GMT
Hi

Based on my understanding, you will have multiple webbrowser controls
instance on the tab control simultaneously.
I think you may try to initialize all the webbrowser control instances in
the tab control's load event just as I last post.

Also from the exception it seems that when we try to dispose the webbrowser
control, the instance has been disposed which caused the problem.\
You may check if there is any code in your program will dispose the
webbrowser.

If my above suggestion did not work for you, would you please simplify your
reproduce sample as possible.(e.g. if the html operation did not affect
reproduce the problem, we can just comment them out temporarily and send to
me via removing "online" from my email address, so that we do can further
troubleshooting.

Thanks for your efforts.

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

"Peter Huang" [MSFT] - 22 Jun 2005 07:52 GMT
Hi Greg,

Based on my test, it seems that the sample works fine at my side.
Here is my reproduce steps.
1. Add the two projects into a test solution
2. compile and press F5/ Ctrl+F5 to run the project
3. Click button1 to load tabpage1/2
4. close the application
5. close the ide
I did not get any exception, did I miss some thing?

Also I wonder why you need to do the dispose method.
Commonly dispose is used to release the unmanaged resource.
Althrough Webbrowser control is unmanaged component, but the RCW layer
which is the layer between .NET and COM will do the release job.
so commonly we do not need dispose here.

Also I have add more button in the test form and try to call the
browserControl1.Dispose method, and the control is disposed and have no
exception.

private void button1_Click(object sender, System.EventArgs e) {
browserControl1.SetContent("<html><body>Hello. How are you?</body></html>");
browserControl2.SetContent("<html><body>I'm fine thanks, how are
you?</body></html>");
}

private void button2_Click(object sender, System.EventArgs e)
{
    this.browserControl1.Dispose();
}

Did I miss something?

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.