Skip to content

Instantly share code, notes, and snippets.

@ericis
Created January 28, 2015 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericis/b337ed89d55cf7c017e6 to your computer and use it in GitHub Desktop.
Save ericis/b337ed89d55cf7c017e6 to your computer and use it in GitHub Desktop.
HTML Button Event Listener for System.Windows.Forms.WebBrowser
namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
public class Program
{
private WebBrowser browser;
private bool isAttached;
[STAThread]
public static int Main(string[] args)
{
var returnCode = 0;
try
{
Console.WriteLine("Running...");
new Program().Run();
}
catch (Exception ex)
{
Console.BackgroundColor = ConsoleColor.Red;
Console.WriteLine("Error: " + ex.Message);
returnCode = -1;
}
Console.ReadLine();
return returnCode;
}
public void Run()
{
Form form = null;
try
{
form = new Form
{
Text = "Web Browser Events",
WindowState = FormWindowState.Maximized
};
var panel = new Panel
{
Dock = DockStyle.Bottom,
Height = 400
};
this.browser = new WebBrowser
{
Dock = DockStyle.Fill,
Url = new Uri("http://checkin.usairways.com")
};
panel.Controls.Add(this.browser);
form.Controls.Add(panel);
var btnAttachBrowserEvent = new Button
{
Text = "Click to attach browser event...",
Dock = DockStyle.Top
};
btnAttachBrowserEvent.Click += this.btnAttachBrowserEvent_Click;
form.Controls.Add(btnAttachBrowserEvent);
form.ShowDialog();
}
finally
{
if (form != null && !form.IsDisposed)
{
form.Dispose();
}
}
}
private void btnAttachBrowserEvent_Click(object sender, EventArgs e)
{
if (!this.isAttached)
{
var btnInternalBrowserButton = this.browser.Document.GetElementById("LookupButton");
if (btnInternalBrowserButton == null)
{
MessageBox.Show("Couldn't find the internal browser button. Please ensure the page has fully loaded");
return;
}
btnInternalBrowserButton.Click += this.btnInternalBrowserButton_Click;
this.isAttached = true;
}
MessageBox.Show("Please click the check-in button to see this working");
}
private void btnInternalBrowserButton_Click(object sender, HtmlElementEventArgs e)
{
MessageBox.Show("Button inside of the browser was clicked");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment