Help With WebBrowser Control

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 6843 Views - Last Post: 22 January 2010 - 03:16 PM Rate Topic: -----

#1 AMDKilla  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 88
  • Joined: 30-December 09

Help With WebBrowser Control

Posted 06 January 2010 - 01:13 PM

I've found many a solution to my coding problems on here, but I have finally come across something that as far as I know hasnt been discussed yet.

I have a WebBrowser control that is programatically created, and I have got a ContextMenuStrip working for it.

What I need to do is be able to distinguish whether the user has clicked a link or not when they right click, so I can have options on the context menu that can use the link to perform actions, such as opening the link in another WebBrowser control.

Here is the code for the generated WebBrowser:
Dim Browse As New WebBrowser
		WebTabs.TabPages.Add(1, "New Tab")
		WebTabs.SelectTab(t - 1)
		Browse.Name = "WebSpace"
		Browse.Dock = DockStyle.Fill
		Browse.IsWebBrowserContextMenuEnabled = False
		Browse.ContextMenuStrip = BrowserContextMenu
		AddHandler Browse.DocumentCompleted, AddressOf DocumentCompletedHandler
		AddHandler Browse.CanGoBackChanged, AddressOf WebSpace_CanGoBackChanged
		AddHandler Browse.CanGoForwardChanged, AddressOf WebSpace_CanGoForwardChanged
		AddHandler Browse.StatusTextChanged, AddressOf WebSpace_StatusTextChanged
		AddHandler Browse.ProgressChanged, AddressOf WebSpace_ProgressChanged
		WebTabs.SelectedTab.Controls.Add(Browse)
		CType(WebTabs.SelectedTab.Controls.Item(0), WebBrowser).Navigate(HomePage)
		t = t + 1


Please help me out, as this one has had me stuck all day :angry:

Is This A Good Question/Topic? 0
  • +

Replies To: Help With WebBrowser Control

#2 AMDKilla  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 88
  • Joined: 30-December 09

Re: Help With WebBrowser Control

Posted 06 January 2010 - 02:22 PM

OK, let me clarify, I need to be able to detect what is clicked on when the context menu is activated, whether it is a hyperlink, or an image, or anything.
Was This Post Helpful? 0
  • +
  • -

#3 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Help With WebBrowser Control

Posted 06 January 2010 - 05:07 PM

This is hard to achieve, but I've done it before, so let me show you.

We basically have to make sure that we can get to the browser events (in your browser we can't because it is dynamically created). What you need to do is this:

In your main form, after the end off ALL of the code (after the 'End Class' for your form) type this

Public Class AxWebbrowser
Inherits Webbrowser
'Here is where we can access the events for the webbrowser!!! Instead of using AddHandler for each one.


End Class



Now change your AddTab Code to this:

Dim Browse As New AxWebbrowser
		WebTabs.TabPages.Add(1, "New Tab")
		WebTabs.SelectTab(t - 1)
		Browse.Name = "WebSpace"
		Browse.Dock = DockStyle.Fill
		Browse.IsWebBrowserContextMenuEnabled = False
		Browse.ContextMenuStrip = BrowserContextMenu
		WebTabs.SelectedTab.Controls.Add(Browse)
		CType(WebTabs.SelectedTab.Controls.Item(0), WebBrowser).Navigate(HomePage)
		t = t + 1




Now comes the complex part:


Add this code in the AxWebbrowser Class

	Public Shared MPoint As Point
	Public Shared Oelement As HtmlElement
	Public WithEvents oDoc As HtmlDocument
	Private Sub oDoc_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs) Handles oDoc.MouseDown
		MPoint = New Point(e.MousePosition.X, e.MousePosition.Y)
		Oelement = oDoc.GetElementFromPoint(MPoint)
	End Sub

	Private Sub AxWebbrowser_DocumentComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles Me.DocumentCompleted
		oDoc = Me.Document
End Sub



You may not understand all the code above, but basically, It means whenever you click, there will be a HtmlElement named Oelement which will be at the location of the mouse.



Now to add the code for the Open Link in New Tab context menu:

Dim Link as String = AxWebbrowser.Oelement.GetAttribute("href")
Dim Browse As New AxWebbrowser
		WebTabs.TabPages.Add(1, "New Tab")
		WebTabs.SelectTab(t - 1)
		Browse.Name = "WebSpace"
		Browse.Dock = DockStyle.Fill
		Browse.IsWebBrowserContextMenuEnabled = False
		Browse.ContextMenuStrip = BrowserContextMenu
		WebTabs.SelectedTab.Controls.Add(Browse)
		CType(WebTabs.SelectedTab.Controls.Item(0), WebBrowser).Navigate(Link)
		t = t + 1




<<<If you have any problems with this code then please don't hesitate to message me or to ask again on the forums>>>

I have made webbrowsers with loads of advanced features like this, And If you're serious about making a professional-style browser, I reccomend looking at the IE7 Clone by Thomas Maxwell (it's where I adapted most of this code from).
Was This Post Helpful? 1
  • +
  • -

#4 AMDKilla  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 88
  • Joined: 30-December 09

Re: Help With WebBrowser Control

Posted 07 January 2010 - 02:18 AM

I already have a copy of IE7 Clone, it's what inspired me to create my own. Not a copy of IE7Clone, but my own browser.

Thanks for helping me out. Ill try your code as soon as I can. Reading through it looks like it will work :)

EDIT: Am I right in thinking I can move all my AddHandler events into the new class? I think I will definatly have to move the code for the DocumentCompleted event, as I have put an AddHandler for that in each new tab code, if that makes sense.

This post has been edited by AMDKilla: 07 January 2010 - 02:25 AM

Was This Post Helpful? 0
  • +
  • -

#5 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Help With WebBrowser Control

Posted 07 January 2010 - 05:48 AM

Yep, You are right in thinking that!
Was This Post Helpful? 0
  • +
  • -

#6 AMDKilla  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 88
  • Joined: 30-December 09

Re: Help With WebBrowser Control

Posted 07 January 2010 - 09:48 AM

OK, So I have the code working, right clicking a link, and then selecting Open in new tab opens the link in a new tab. But what I am trying to do now is disable the new tab menu option when the user right clicks on something other than a link.

I tried something like:

Private Sub BrowserContextMenu_Opening(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles BrowserContextMenu.Opening
		If AxWebbrowser.Oelement.GetType() = Not "Link" Then
			ContextOpenNewTab.Enabled = False
		End If
	End Sub


It throws up "Overload resolution failed because no accessible '=' can be called with these arguments:
'Public Shared Operator =(a As String, b As String) As Boolean': Value of type 'System.Type' cannot be converted to 'String'."
Was This Post Helpful? 0
  • +
  • -

#7 AMDKilla  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 88
  • Joined: 30-December 09

Re: Help With WebBrowser Control

Posted 08 January 2010 - 11:48 AM

Nevermind, I fixed it.

Private Sub BrowserContextMenu_Opening(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles BrowserContextMenu.Opening
		Dim Link As String = AxWebbrowser.Oelement.GetAttribute("href")
		If Link = Nothing Then
			'Hide Some Menu Contents
		Else : 'Show Some Menu Contents
		End If
	End Sub

Was This Post Helpful? 0
  • +
  • -

#8 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Help With WebBrowser Control

Posted 08 January 2010 - 12:33 PM

Hmm, perhaps a better way to do it would be like this:

			If oElement.TagName = "A" Then
	   'Show menu contents
			Else
	   'Hide menu contents		   
			End If

'We can also use this code to check for images, using a code like 'this one:
		 If oElement.TagName = "IMG" Then
			  'Show a context meny such as view image
			  'To implement a context menu like this, we would need to use Oelement.GetAttribute("src")
			Else
  'Hide an image context menu
			End If



Was This Post Helpful? 1
  • +
  • -

#9 AMDKilla  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 88
  • Joined: 30-December 09

Re: Help With WebBrowser Control

Posted 08 January 2010 - 04:44 PM

View PostJack Eagles1, on 8 Jan, 2010 - 11:33 AM, said:

Hmm, perhaps a better way to do it would be like this:

			If oElement.TagName = "A" Then
	   'Show menu contents
			Else
	   'Hide menu contents		   
			End If

'We can also use this code to check for images, using a code like 'this one:
		 If oElement.TagName = "IMG" Then
			  'Show a context meny such as view image
			  'To implement a context menu like this, we would need to use Oelement.GetAttribute("src")
			Else
  'Hide an image context menu
			End If




I had to change the code to:
			If AxWebbrowser.oElement.TagName = "A" Then
	   'Show menu contents
			Else
	   'Hide menu contents		   
			End If

'We can also use this code to check for images, using a code like 'this one:
		 If AxWebbrowser.oElement.TagName = "IMG" Then
			  'Show a context meny such as view image
			  'To implement a context menu like this, we would need to use Oelement.GetAttribute("src")
			Else
  'Hide an image context menu
			End If




Just adding the AxWebbrowser in front of the oElement.TagName

Thanks for all your help. I think I'm starting to get the hang of vb.net now :D
Was This Post Helpful? 0
  • +
  • -

#10 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Help With WebBrowser Control

Posted 08 January 2010 - 05:36 PM

Ok, glad to hear it :) post more topics / message me if you need any more help with webbrowsers. You may also want to take a look at HydraBrowser, which was programmed in VB.NET. It's an amazingly compliceted Office 2007 styled browser in .NET. I have had a look at it's source code (using reflector decompiler), and you might benefit from some of the code in there (although it is really huuuuge).
Was This Post Helpful? 0
  • +
  • -

#11 AMDKilla  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 88
  • Joined: 30-December 09

Re: Help With WebBrowser Control

Posted 09 January 2010 - 06:57 AM

Well I have a nother problem now. I would have posted this in a new topic, but it uses some of the code here, so rather than explain the code again, it just follows on from above.

I have successfully got the links opening in new tabs, but I am having trouble opening new windows with them.

Private Sub ContextOpenNewWindow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextOpenNewwindow.Click
		Dim Link As String = AxWebbrowser.Oelement.GetAttribute("href")
		Dim NewWindow As New MainWindow()
		CType(Newwindow.WebTabs.Controls.Item(0), WebBrowser).Navigate(Link)
	End Sub



I get the error "Index 0 is out of range. Parameter name: index". This happens whenever I click the Open in New Window Button.

I have a feeling this is because the newly created instance of MainWindow can't access the value of Link.

I tried defining Link as a Public variable, but it threw up an exception before the first form could even be created. The Error is "Object reference not set to an instance of an object."
Was This Post Helpful? 0
  • +
  • -

#12 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Help With WebBrowser Control

Posted 09 January 2010 - 08:47 AM

Hmm... Try making NewWindow Show before asking it to navigate... I'll have a look into this... it's a long shot, but I'll get back to you.

Did you just do Public Link as string, or did you do Public Shared link as string?

This post has been edited by Jack Eagles1: 09 January 2010 - 08:48 AM

Was This Post Helpful? 0
  • +
  • -

#13 AMDKilla  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 88
  • Joined: 30-December 09

Re: Help With WebBrowser Control

Posted 09 January 2010 - 08:50 AM

View PostJack Eagles1, on 9 Jan, 2010 - 07:47 AM, said:

Hmm... Try making NewWindow Show before asking it to navigate... I'll have a look into this... it's a long shot, but I'll get back to you.

Did you just do Public Link as string, or did you do Public Shared link as string?


I tried showing the new form before asking it to navigate, and got the following error: "Unable to cast object of type 'System.Windows.Forms.TabPage' to type 'System.Windows.Forms.WebBrowser'".

I did Public Link As String, I never tried Public Shared Link As String. Ill try that now :)
Was This Post Helpful? 0
  • +
  • -

#14 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Help With WebBrowser Control

Posted 09 January 2010 - 08:59 AM

Another reason why your program isn't working could be that you havent told your New Window to make a new tab before telling it to navigate to the website.




Ok, I've thought of a really messy way to do it (if Public Shared hasn't fixed it), which could come in usefull untill I find a long term soloution. Here it is:



Private Sub ContextOpenNewWindow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextOpenNewwindow.Click
		Dim Link As String = AxWebbrowser.Oelement.GetAttribute("href")
		Dim SaveF As New System.IO.StreamWriter(My.Application.StartUpPath & "\NewLink")
		Savef.Writeline(Link)
		SaveF.Close
		Dim Ofrm as new MainWindow
		Ofrm.Show
	End Sub





Next we can Put this code in the MainWindow_Load event.

If My.Computer.FileSystem.FileExists(My.Application.StartUpPath & "\NewLink") Then
Dim LoadF as New System.IO.StreamReader("My.Application.StartUpPath & "\NewLink")
Dim Link as String = Loadf.Readline
Loadf.Close
CType(Newwindow.WebTabs.Controls.Item(0), WebBrowser).Navigate(Link)




EDIT: You might want to delete the file after using it...

This post has been edited by Jack Eagles1: 09 January 2010 - 09:02 AM

Was This Post Helpful? 0
  • +
  • -

#15 AMDKilla  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 88
  • Joined: 30-December 09

Re: Help With WebBrowser Control

Posted 09 January 2010 - 09:06 AM

Problem Solved!!

I changed the code to the following:


Private Sub ContextOpenNewWindow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextOpenNewwindow.Click
		Dim Link As String = AxWebbrowser.Oelement.GetAttribute("href")
		Dim NewWindow As New MainWindow()
		Newwindow.Show()
		CType(Newwindow.WebTabs.SelectedTab.Controls.Item(0), WebBrowser).Navigate(Link)
	End Sub




I had forgotten to include SelectedTab in the CType part :D
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2