I'm new to programming, and was trying to create a browser (using the web browser control) that connected through a TCP client. I soon realized that I would need to trap the links on the page, so I could redirect them through my application. A little bit of time and coffee later, I realized that no one appears to have ever needed this. Or at the very least, I was unable to find any information on it. However, I eventually gave up and decided to give it a shot myself. I am trying to get better with my programming, and would really like to try and make a career out of it, so please comment on this and let me know what you think or where I can improve.
Description of the solution
The web browser control shipped with the .NET Framework provides many useful functions and properties. As well as this, many of the events are available without too much fiddling around. First off, I monitored the
DocumentCompleted event, and then made any changes to the document that I required. In this case, I used a
HTMLElementCollection object to store all the links on the page. I then created an event handler,
LinkClicked, and assigned it to the
OnClick event of all links. This way, I was aware of any navigation that was occurring. I'll explain the code first.
Setting up the event handler is as simple as this:
Private Sub webControl_DocumentCompleted(ByVal sender As Object, _
ByVal e As WebBrowserDocumentCompletedEventArgs) _
Dim link As HtmlElement
Dim links As HtmlElementCollection = webControl.Document.Links
For Each link In links
If btnChange.Checked Then
link.AttachEventHandler("onclick", AddressOf LinkClicked)
This code gets all the links on the page, and then assigns the
LinkClicked event handler to the '
OnClick' event of those links.
Private Sub LinkClicked(ByVal sender As Object, ByVal e As EventArgs)
Dim link As HtmlElement = webControl.Document.ActiveElement
Dim url As String = link.GetAttribute("href")
MsgBox("Link Clicked: " & link.InnerText & _
vbCrLf & "Destination: " & url)
Now we know that a link was clicked. So next, I simply grab the document's currently active item (the link) and read in the '
Navigating event occurs before the page is loaded, so it's the best place to cancel any navigation we don't want to occur:
Private Sub webControl_Navigating(ByVal sender As Object, _
ByVal e As WebBrowserNavigatingEventArgs) _
If btnCancel.Checked Then
e.Cancel = True
Shaps currently works as the Senior Networks Administrator for a small IT company in Australia. He is also involved in various software developments, for Windows, Windows Mobile, iPhone, Mac and occasionally the web. His central focus in this area, is User Interface design.
If he had spare time, he'd play some tunes, read a book or visit the gym. Until then, he writes small applications for the iPhone that help improve his productivity at work.