WebBrowser Control - Which element was clicked

B

Bill

I have a form with a WebBrowser control. In the DocumentComplete event I add
an OnClick handler for each link found in the document. The OnClick for each
link calls the same handler function.

The events fire and this works great. The problem I have is determining
which link was clicked.

I would like to be able to get an instance of the specific HTMLElement
object that was clicked in the handler function.

The toElement and srcElement properties of the HtmlElementEventArgs object
are always null.

I would appreciate any ideas.


private void webBrowser_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
HtmlElementCollection tags = webBrowser.Document.Links;
foreach(HtmlElement element in tags)
element.Click += new
HtmlElementEventHandler(element_Click);
}

void element_Click(object sender, HtmlElementEventArgs e)
{
HTMLElement ClickedElement = ?????
}
 
N

Nicholas Paldino [.NET/C# MVP]

Bill,

Well, the element that is clicked is the sender parameter, you just have
to cast it to an HTMLElement and viola.
 
B

Bill

Too easy for me... Thank you

--
Bill


Nicholas Paldino said:
Bill,

Well, the element that is clicked is the sender parameter, you just have
to cast it to an HTMLElement and viola.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Bill said:
I have a form with a WebBrowser control. In the DocumentComplete event I
add
an OnClick handler for each link found in the document. The OnClick for
each
link calls the same handler function.

The events fire and this works great. The problem I have is determining
which link was clicked.

I would like to be able to get an instance of the specific HTMLElement
object that was clicked in the handler function.

The toElement and srcElement properties of the HtmlElementEventArgs object
are always null.

I would appreciate any ideas.


private void webBrowser_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
HtmlElementCollection tags = webBrowser.Document.Links;
foreach(HtmlElement element in tags)
element.Click += new
HtmlElementEventHandler(element_Click);
}

void element_Click(object sender, HtmlElementEventArgs e)
{
HTMLElement ClickedElement = ?????
}
 
P

Peter Bromberg [C# MVP]

Another violin player! A viola is a musical instrument. The expression which
means “behold!†is voilà - meaning “look there!, from the French.
:)

Site: www.eggheadcafe.com
UnBlog: petesbloggerama.blogspot.com
Metafinder: www.blogmetafinder.com



Nicholas Paldino said:
Bill,

Well, the element that is clicked is the sender parameter, you just have
to cast it to an HTMLElement and viola.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Bill said:
I have a form with a WebBrowser control. In the DocumentComplete event I
add
an OnClick handler for each link found in the document. The OnClick for
each
link calls the same handler function.

The events fire and this works great. The problem I have is determining
which link was clicked.

I would like to be able to get an instance of the specific HTMLElement
object that was clicked in the handler function.

The toElement and srcElement properties of the HtmlElementEventArgs object
are always null.

I would appreciate any ideas.


private void webBrowser_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
HtmlElementCollection tags = webBrowser.Document.Links;
foreach(HtmlElement element in tags)
element.Click += new
HtmlElementEventHandler(element_Click);
}

void element_Click(object sender, HtmlElementEventArgs e)
{
HTMLElement ClickedElement = ?????
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top