DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: [SOLVED] C# WebBrowser Event not firing!  (Read 6,261 times)

Asudem

  • C# data manipulation junkie
  • Member
  • Joined in 2015
  • **
  • Posts: 132
[SOLVED] C# WebBrowser Event not firing!
« on: March 06, 2016, 04:31 PM »
Hey kids, it's your uncle Asudem here with some sage like advice.

If you have an event that needs to fire, but also assign that event in the initialization of the form, make sure you have some kind of check to not to add the initialized event the second time around.

Alright guys, I'm at my wit's end.

I have a Winform with a WebBrowser control, and once a page loads, it fires an event. Everything works correctly and as intended.

If I create a new instance of that Winform, then load a page into the WebBrowser on the newly created Winform, the same code which assigns the WebBrowser event does not fire....

For example

Code: ( csharp)
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Show();
        }
        
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            MessageBox.Show(webBrowser1.Url.ToString());
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://www.google.com");
            webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
        }
    }
Code: ( csharp)
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();
            form1.Show();
        }
    }
When Form2 creates a new instance of Form1, there is no messagebox once the navigated url is loaded... I am dumbfounded... Any ideas what might be going wrong?
If I do it more than 2 times I want to automate it in C#!
« Last Edit: March 06, 2016, 04:57 PM by Asudem »

Original address: https://www.donationcoder.com/forum/index.php?topic=42392.0