Can’t close a popup from webbrowser control

I tried to implement this answer http://stackoverflow.com/a/6473442/4085725 to handle the popup in a new winform.
But i don’t know how i can close the popup after i have done my job and how i can set a delay when the browser click on elements (I’ve tried to set async, but i receive an error “Async methods cannot have ref parameters).
So my questions is how to close the new form after I have done my job and how i can set a delay.
Here is partial code:

void nativeBrowser_NewWindow2(ref object ppDisp, ref bool Cancel)
 {
    var popup = new Form2();
    popup.Browser.ScriptErrorsSuppressed = true;
    popup.Show(this);
    ppDisp = popup.Browser.ActiveXInstance;
    //Here I need a delay
    popup.Browser.Document.GetElementById("pass").SetAttribute("value", "password");
    // Here I need other delay
    // here I want to close the popup form

}

And here is the full code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Permissions;
using System.Runtime.InteropServices;
using SHDocVw;

namespace alta_solutie
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SHDocVw.WebBrowser nativeBrowser;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            nativeBrowser = (SHDocVw.WebBrowser)webBrowser.ActiveXInstance;
            nativeBrowser.NewWindow2 += nativeBrowser_NewWindow2;

        }
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            nativeBrowser.NewWindow2 -= nativeBrowser_NewWindow2;
            base.OnFormClosing(e);
        }

        void nativeBrowser_NewWindow2(ref object ppDisp, ref bool Cancel)
        {
            var popup = new Form2();
            popup.Browser.ScriptErrorsSuppressed = true;
            popup.Show(this);
            ppDisp = popup.Browser.ActiveXInstance;
            //Here i need a delay
            popup.Browser.Document.GetElementById("pass").SetAttribute("value", "password");
            // Here i need other delay
            // here i want to close the popup form

        }


        /// Login on the site
        private async void button1_Click(object sender, EventArgs e)
        {
            webBrowser.Navigate("http://hitserr.com/");
            webBrowser.ScriptErrorsSuppressed = true;
            while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
            }
            await Task.Delay(1000);
            SetParamethers.SendTextNoId(webBrowser, "input", "name", "login", "Username");
            SetParamethers.SendTextNoId(webBrowser, "input", "name", "pass", "password");
            SetParamethers.ClickNoId(webBrowser, "input", "name", "connect");
            await Task.Delay(1000);
            foreach (System.Windows.Forms.HtmlElement html in webBrowser.Document.GetElementsByTagName("a"))
            {
                if (html.InnerText == "Facebook Likes")
                {
                    html.InvokeMember("Click");

                    break;
                }
            }   
        }

        // Click on the button who create the popup window
        private void button2_Click(object sender, EventArgs e)
        {
            foreach (System.Windows.Forms.HtmlElement html in webBrowser.Document.GetElementsByTagName("a"))
            {
                if (html.InnerText == "Like")
                {
                    // when html invoke Click, the window appears
                    OnLoad(e);
                    html.InvokeMember("Click"); 
                    break;
                }
            }
        }

    }
}

Thank you and I will really appreciate your answers.


Source: forms

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.