Cannot navigate to facebook.com from WebBrowser Tool in VB.NET - vb.net

I tried to navigate to www.google.com and it worked, I even tried to navigate to www.yahoo.com and it still worked!
But when I tried to navigate to www.facebook.com it doesn't show anything.
Here's the code:
Public Class Simple
Private Sub Simple_Load(sender As Object, e As EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("https:\\www.facebook.com")
End Sub
End Class
The above code worked well but when I changed the parameter with "https:\www.facebook.com" it didn't work.
I don't know what is causing this?
Maybe it can't load javascript or something (just a guess)
Output when WebBrowser1.Navigate("https:\www.google.com")
Output when WebBrowser1.Navigate("https:\www.google.com")

WebBrowser is outdated which can cause issue with javascript and other newer libraries. I have just started to used GeckoFx which uses Firefox and not dependent on IE. Use Nuget Console "Install-Package Geckofx45 -Version 45.0.34"
Here's an example how to browse Facebook via GeckoFX
Private geckoWebBrowser As Gecko.GeckoWebBrowser
Private Sub WebView_Load(sender As Object, e As EventArgs) Handles MyBase.Load
InitializeBrowser()
End Sub
Private Sub InitializeBrowser()
Gecko.Xpcom.Initialize("Firefox")
geckoWebBrowser = New Gecko.GeckoWebBrowser()
geckoWebBrowser.Dock = DockStyle.Fill
Me.Controls.Add(geckoWebBrowser)
geckoWebBrowser.Navigate("https://www.facebook.com")
End Sub

Related

Simple application with opening webpage in VB

I would like to create simple exe app in VB, which will open default browser with specified page. I have this code
Public Class Form1
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
System.Diagnostics.Process.Start("www.google.com")
End Sub End Class
If I click on the button I get error message:
System.ComponentModel.Win32Exception: System cannot find this file
Could someone help me where is the problem? Or is there any other approach how to open webpage?
"System cannot find this file" means that your system is not reading the URL as one and it's trying to get the resource at the local path: "www.google.com" which obviously doesn't exist. Check this link because the issue seems to be the same.
Here in an application I call Best Links is the process I use to view URL's.
It works by storing website URL links in a SQLite Database (DB).
The DB also stores the Site Name and the Last Visit Date.To view the site the user just clicks on the DB entry displayed in DataGridView. Screen Shot Below. And the code that opens the link in what ever your default browser is.
Private Sub dgvLinks_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvLinks.CellClick
selRow = e.RowIndex
If e.RowIndex = -1 Then
gvalertType = "4"
frmAlert.ShowDialog()
Exit Sub
End If
Dim row As DataGridViewRow = Me.dgvLinks.Rows(e.RowIndex)
If row.Cells(2).Value Is Nothing Then
gvalertType = "5"
frmAlert.ShowDialog()
Return
Exit Sub
ElseIf gvTxType = "View" Then
webPAGE = row.Cells(2).Value.ToString()
siteID = CInt(row.Cells(0).Value.ToString())
UpdateSiteData()
Process.Start(webPAGE)
Data Grid View
If you would like the complete code or and exe file let me know and I will add it to my GitHub repository code will be a ZIP file.

.NET 5.0 WinForms Button_Click event code not running & multiple issues

Issue
I'm trying to Debug an application form with buttons. Usually when we double click the button, the source window shows up with the click event for that button. However, in the latest version of .NET 5.0 release, there are a lot of things not working properly.
The following are multiple issues I'm facing that usually are not there and there is no explanation on Microsoft's Blog or MSDN about these issues:
When I'm adding the source-code to any button_click event, nothing is hapenning after build and debug! I tried to make it work but to no effort.
After renaming control(s) on the form, the project needs a complete rebuild to change the names of the controls in Designer.vb file. This used to be automatic in VB prior to .NET 5.0
Application.Designer.vb randonly desides to default the Me.MainForm = Global.<ProjectNameHere>.<FormNameHere> to Me.MainForm = Global.<ProjectNameHere>.Form1 & then throws an error that Form1 is not found. Why the hell this extra work? VB used to set this automatically.
Code: Button Click Events
Public Class MainWindow
Private Sub Btn_Exit_Click(sender As Object, e As EventArgs)
Application.Exit()
End Sub
Private Sub Btn_MaxMin_Click(sender As Object, e As EventArgs)
If WindowState = FormWindowState.Normal Then
WindowState = FormWindowState.Maximized
Else
WindowState = FormWindowState.Normal
End If
End Sub
Private Sub Btn_Minimize_Click(sender As Object, e As EventArgs)
WindowState = FormWindowState.Minimized
End Sub
End Class
I tried Debugging and click events don't respond. I tried to rebuild solution and it doesn't work. I tried adding Refresh() after each line of code and doesn't work. I rechecked designer.vb and everything else and it doesn't work.
.NET 5.0 is broken when using Visual Basic WinForms. The code intellisense has become slow. Everything is slow and not working.
System Information
Product Version: Microsoft Visual Studio Community 2019 16.8.0
DotNET Framework: 5.0 (Released on 11 November 2020)
Operating System: Windows 10 20H2
Please help me fix these issues. No code provided because I don't know how will other devs reproduce these issues. I cannot find any similar problems in SO or anywhere else. I can't find any problem when using .NET 4.8 or other .NET versions other than 5.0 with WinForms and VB.
Looking at your button click event handler code, I see that there is no Handles Button1.Click.
In order for the code to run, it must be associated with the event by doing:
Private Sub Btn_Exit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Application.Exit()
End Sub
Another option is:
Private Sub Form1_Load(sender as Object, e as EventArgs) Handles Me.Load
AddHandler btnExit.Click, AddressOf Btn_Exit_Click 'run this somewhere when the form loads
End Sub
Private Sub Btn_Exit_Click(sender As Object, e As EventArgs)
Application.Exit()
End Sub
If this is helpful, great. If not, provide more of your code so we can see what's all happening.
Regards

VB.net Gecko WebBrowser DocumentCompleted not compatible?

I'm using GeckoBrowser, as I find it displays and loads pages faster than the native WebBrowser(IE). The only issue I'm having is when to tell the page has finished loading so I can then run more code.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
GeckoWebBrowser1.Navigate("http://www.google.com")
End Sub
Private Sub GeckoWebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles GeckoWebBrowser1.DocumentCompleted
Label1.Text = "YES"
End Sub
End Class
The error I get is "Method 'GeckoWebBrowser1_DocumentCompleted' cannot handle event 'DocumentCompleted' because they do not have a compatible signature.
I've tried changing 'WebBrowserDocumentEventArgs' to 'GeckoDocumentCompletedEventArgs' but that says 'Type - is not defined'.
Any suggestions would be greatly appreciated.
#jmcilhinney's solution worked, just used drop downs at the top.
All I really needed was to add Imports Gecko.Events at the top

Wait for internet explorer to load everything?

I am scraping a webpage, and waiting for internet explorer to get done loading but for some reason it is not. I'm trying to get a value on the page but the wait part is not waiting therefore the value comes back blank when there should be a value. The IE page has done loading but the value for the elements on the page has not been loaded yet. Is there a way to wait for all elements to get done loading before proceeding to next line of code? Here's my code:
Dim IE As Object
Dim myvalue as string
IE = CreateObject("internetexplorer.application")
IE.navigate("mypage")
While Not IE.ReadyState = WebBrowserReadyState.Complete
Application.DoEvents()
End While
myValue = IE.document.getElementById("theValue").getAttribute("value")
Debug.Print(myValue)
You SHOULD NOT use Application.DoEvents() in order to keep your UI responsive! I really can't stress this enough! More than often using it is a bad hack which only creates more problems than it solves.
For more information please refer to: Keeping your UI Responsive and the Dangers of Application.DoEvents.
The correct way is to use the InternetExplorer.DocumentComplete event, which is raised when the page (or a sub-part of it, such an an iframe) is completely loaded. Here's a brief example of how you can use it:
Right-click your project in the Solution Explorer and press Add Reference...
Go to the COM tab, find the reference called Microsoft Internet Controls and press OK.
Import the SHDocVw namespace to the file where you are going to use this, and create a class-level WithEvents variable of type InternetExplorer so that you can subscribe to the event with the Handles clause.
And voila!
Imports SHDocVw
Public Class Form1
Dim WithEvents IE As New InternetExplorer
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
IE.Navigate("http://www.google.com/")
End Sub
Private Sub IE_DocumentComplete(pDisp As Object, ByRef URL As Object) Handles IE.DocumentComplete
MessageBox.Show("Successfully navigated to: " & URL.ToString())
End Sub
End Class
Alternatively you can also subscribe to the event in-line using a lambda expression:
Imports SHDocVw
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim IE As New InternetExplorer
IE.Navigate("http://www.google.com/")
AddHandler IE.DocumentComplete, Sub(pDisp As Object, ByRef URL As Object)
MessageBox.Show("Successfully navigated to: " & URL.ToString())
End Sub
End Sub
End Class

How do I invoke a click event with GeckoFx?

I can't seem to invoke a click event in GeckoFX in vb.net. Here is my code:
Imports Skybound.Gecko
Public Class Form1
Sub New()
InitializeComponent()
Xpcom.Initialize(Environment.CurrentDirectory + "/xulrunner")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
GeckoWebBrowser1.Navigate("https://www.google.com")
Dim Button = New GeckoButtonElement(_webBrowser.Document.getElementById("Search").DomObject)
End Sub
End Class
The problem is that it says I do not have GeckoButtonElement class. I followed this tutorial.
Can someone help me get this button to click in another way? Or is there something else I have to do?
Side question: is there a package with the latest geckofx + xulrunner for download somewhere? I can't seem to find anything newer than what was offered in the tutorial above (which is from 2012).
Thanks.
Dim button As Gecko.DOM.GeckoButtonElement = TryCast(_webBrowser.Document.GetElementById("Search"), Gecko.DOM.GeckoButtonElement)
If button IsNot Nothing Then
button.Click()
End If
Newer versions of GeckoFx available on https://bitbucket.org/geckofx
Latest version is GeckoFx 29.0 (https://bitbucket.org/geckofx/geckofx-29.0/downloads).