Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Web Browser Control blocks parent's Load event

This may come across as incredibly stupid, but I cannot figure out if:

  • I am an idiot
  • I have misunderstood something
  • The MS Web Browser control is bugged

I prefer to think that it is the latter.

I have a Web Browser control in a WinForms user control. It has been added to the control at design time, and in theory, in the Load event of the control it should navigate to Google.

Seems straightforward.

However.

public partial class TVHost : UserControl
{
    public TVHost()
    {
        InitializeComponent();
    }

    private void TVHost_Load(object sender, EventArgs e)
    {     
        webBrowser1.Navigate("http://google.co.uk");  
    }
}

This doesn't work. No error, just nothing. Inserting a breakpoint/debug lines shows me that the Load event doesn't even get called.

I decided at this point to check that the Load event is being set correctly in the Designer.cs file.

 this.Load += new System.EventHandler(this.TVHost_Load);

Seems legit.

If I remove the web browser control from the form, the load event fires.

I don't understand this one bit, how can a control prevent a method which uses it from firing in the first place?

Moving on, I found this: http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/d6e427b2-9cc9-4318-bb05-11363025e3f7/

TL;DR for the link is as follows: "Load won't work if you have a webbrowser on the form which is set to Visible = true"

So sure as hell, if I change the default visibility of the webbrowser to false, the load event of the control fires. I can work around the problem by setting the visibility of the browser in the load event.

private void TVHost_Load(object sender, EventArgs e)
{
    webBrowser1.Visible = true;
    webBrowser1.Navigate("http://google.co.uk");
}

Very odd.

Whilst this "fix" works, I find it incredibly hacky and was wondering if anybody has any explaination for this behaviour?

Amazingly I have found this bug in MS Connect, left over from 2005 - http://connect.microsoft.com/VisualStudio/feedback/details/116535/when-adding-a-webbrowser-control-to-a-user-control-the-load-will-not-fire#

like image 565
KingCronus Avatar asked May 16 '12 10:05

KingCronus


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

From the discussion in the Connect bug you linked to:

For now, if you want to get the Load event to fire, you can set the URL property of the WebBrowser control in the property grid. The URL can be anything you want, even about:blank if you don't want it to start with a page loaded.

So if you go into the designer and set the WebBrowser's Url property to the string about:blank (which tells the WebBrowser to load an empty page), then your user control should start getting its Load event again.

like image 79
Joe White Avatar answered Oct 29 '22 17:10

Joe White