Skip to content

Instantly share code, notes, and snippets.

@akfish
Created October 12, 2011 08:32
Show Gist options
  • Save akfish/1280652 to your computer and use it in GitHub Desktop.
Save akfish/1280652 to your computer and use it in GitHub Desktop.
C# Attach event to HTMLElement in webBrowser
/// <summary>
/// Inject onclick handler to close button
/// </summary>
private void InjectJS()
{
HtmlDocument doc = webContent.Document;
HtmlElement closeBtnElement = doc.GetElementById(CloseButtonId);
if (closeBtnElement == null)
return;
closeBtnElement.AttachEventHandler("onclick", new EventHandler(WebCloseButtonCallBack));
}
/// <summary>
/// Called when close button is clicked
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void WebCloseButtonCallBack(Object sender, EventArgs e)
{
//Do something here
}
@prdpspkt
Copy link

This is nice. But how to detect if an event listener is already attached.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment