WebBrowser ignores the code - vb.net

I am trying to use Mibbit irc in my project, and so far is working well, but there is a flaw. Links pasted in the chat upon click are getting opened in Internet explorer, instead of users' default web browser. I tried implementing a simple code, but half of it seems to get ignored.
http://i.stack.imgur.com/FKGGr.jpg
WebBrowser Component Startup page: http://widget.mibbit.com/?settings=4abcd3a5f0bf25306d4c6d1968e28cb2&server=irc.mibbit.net&channel=%23Mytestchannel12345
Ignore if contains: mibbit.com(the chat widged) & ad4game.com(the stupid banner...)
If contains because it places different banners - thus, different links. As well for the widged, it obviously have several servers that is hosting it on and it redirects to some of them, like widged1.mibbit.com, widged2.mibbit.com, etc.
Open in Default user browser: All, except those 2 above.
Public Class Form1
Private Sub WebBrowser1_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
Dim navTo As String = e.Url.ToString
If Not (navTo.ToLower.Contains("mibbit.com") OrElse navTo.ToLower.Contains("ad4game.com") OrElse navTo.ToLower.Contains("about:blank")) Then
e.Cancel = True
System.Diagnostics.Process.Start(e.Url.ToString())
End If
End Sub
End Class
Nothing so far worked...

Okay, I've updated your code sample:
Add a new function to find out what the path to the default browser is:
Public Class Form1
Private Sub WebBrowser1_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
Dim navTo As String = e.Url.ToString
If Not (navTo.ToLower.Contains("mibbit.com") OrElse navTo.ToLower.Contains("ad4game.com") OrElse navTo.ToLower.Contains("about:blank")) Then
e.Cancel = True
System.Diagnostics.Process.Start(GetDefaultBrowserPath, e.Url.ToString())
End If
End Sub
' get the default folder path from the registry
Public Function GetDefaultBrowserPath() As String
Dim defaultbrowser As String = My.Computer.Registry.GetValue("HKEY_CLASSES_ROOT\HTTP\shell\open\command", "", "Not Found")
Return Split(defaultbrowser, """")(1)
End Function
End Class

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.

VB.NET - Check if webbrowser's url has changed

So basically I am asking how to check if the URL of the webbrowser has changed (is diferrent from the previous one).
Thank you.
You can check ...
Sub webbrowser1_Complete(ByVal sender As Object, _
ByVal e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
//Webbrowser1.url property to get valu of url
End Sub
Ok from what I can understand from your question this is what I would do. First create a structurethis will allow you to store data that you may want to use again. Next create a Function in this case with a Boolean return that checks to see if WebBrowser1's current url is the same as the one we have stored within our structure.And once you have done that, I would create a new WebBrowser1 Event in this case WebBrowser1_DocumentCompleted to trigger the Function to compare both the WebBrowser1 url textbox and the structures stored string when a web page as completely loaded.
Public Class Form1
Dim urlSettings As urlSetting
Structure urlSetting
Public url As String
End Structure
Private Function checkURL(url As String) As Boolean
Dim changed As Boolean = True
If Not urlSettings.url = url Then
changed = False
End If
Return changed
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
WebBrowser1.Navigate(New Uri(TextBox1.Text))
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If Not checkURL(TextBox1.Text) Then
urlSettings.url = TextBox1.Text
MessageBox.Show("The URL has changed")
End If
End Sub
End Class
Of course you can modify so suit your needs, however this should get you on your way. :) MSDN Information Structure: https://msdn.microsoft.com/en-us/library/4ft0z102.aspx WebBrowser Control : https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser(v=vs.110).aspx Return Statesments eg Function: https://msdn.microsoft.com/en-us/library/2e34641s.aspx

VB.NET WebBrowser Control Programmatically Filling Form After Changing User-Agent (Object reference not set to an instance of an object.)

I'm working on a project where I have a WebBrowser control which needs to have a custom user-agent set, then go to Google and fill out the search box, click the search button, then click a link from the search results. Unfortunately I can't use HTTPWebRequest, it has to be done with the WebBrowser control.
Before I added the code to change the user-agent, everything worked fine. Here's the code that I have:
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("urlmon.dll", CharSet:=CharSet.Ansi)> _
Private Shared Function UrlMkSetSessionOption(dwOption As Integer, pBuffer As String, dwBufferLength As Integer, dwReserved As Integer) As Integer
End Function
Const URLMON_OPTION_USERAGENT As Integer = &H10000001
Public Sub ChangeUserAgent(Agent As String)
UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, Agent, Agent.Length, 0)
End Sub
Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
ChangeUserAgent("Fake User-Agent")
wb.Navigate("http://www.google.com", "_self", Nothing, "User-Agent: Fake User-Agent")
End Sub
Private Sub wb_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs) Handles wb.DocumentCompleted
Dim Source As String = wb.Document.Body.OuterHtml
Dim Uri As String = wb.Document.Url.AbsoluteUri
If Uri = "http://www.google.com/" Then
wb.Document.GetElementById("lst-ib").SetAttribute("value", "browser info")
wb.Document.All("btnK").InvokeMember("click")
End If
If Uri.Contains("http://www.google.com/search?") Then
Dim TheDocument = wb.Document.All
For Each curElement As HtmlElement In TheDocument
Dim ctrlIdentity = curElement.GetAttribute("innerText").ToString
If ctrlIdentity = "BROWSER-INFO" Then
curElement.InvokeMember("click")
End If
Next
End If
End Sub
End Class
The problem lies in the following code:
wb.Document.GetElementById("lst-ib").SetAttribute("value", "browser info")
wb.Document.All("btnK").InvokeMember("click")
I thought the problem might be that the page not being fully loaded (frame issue) but I put the offending code in a timer to test, and got the same error. Any help would be much appreciated.
Do you realize .All("btnK") returns a collection? So, you are doing .InvokeMember("click") on a Collection :). You cannot do that, you can only do .InvokeMember("click") on an element for obvious reasons!
Try this:
wb.Document.All("btnK").Item(0).InvokeMember("click")
The .Item(0) returns the first element in the collection returned by .All("btnK"), and since there will only probably be one item returned, since there is only one on the page, you want to do the InvokeMember on the first item, being .Item(0).
May I ask what it is you are developing?
Since you're a new user, please up-vote and/or accept if this answered your question.

How is it possible to parse the URL of the desired popup to the popup-form AND show hints/tooltips in the WebKit-Component?

I'm trying to use the WebKit-component (http://www.webkit.org/) in VB with the help of Visual Studio 2008.
This is running without problems, except for two following two issues:
1. Hints/Tooltips are not shown (e.g. as there usually will appear one if you stay with the mouse over the Google-logo)
2. If there's a popup-window, I don't know how to get the new desired URL.
I'm already working a few days on this matter and couldn't find any solution yet :(
Maybe you know a solution to this problem.
Cheers
Markus G.
P.S.: If you need more than the following Source Code to analyze the problem, then let me know ...
Source Code Form1
Imports System.IO
Imports WebKit
Public Class frmMain
Private _url As String
Private _mode As String
Private _popupUrl As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
On Error Resume Next
Dim bLogging As Boolean
setWindowAndBrowserSettings()
_url = "http://www.google.com"
browserComp.Navigate(_url)
End Sub
Private Sub setWindowAndBrowserSettings()
Me.Text = "Test - Browser"
Me.WindowState = FormWindowState.Maximized
browserComp.Dock = DockStyle.Fill
browserComp.Visible = True
End Sub
Private Sub browserComp_NewWindowCreated(ByVal sender As Object, ByVal e As WebKit.NewWindowCreatedEventArgs) Handles browserComp.NewWindowCreated
'frmPopup.WindowState = FormWindowState.Maximized
frmPopup.Text = "ixserv - POPUP"
frmPopup.popup.Navigate(_popupUrl)
frmPopup.Show()
End Sub
Private Sub browserComp_NewWindowRequest(ByVal sender As Object, ByVal e As WebKit.NewWindowRequestEventArgs) Handles browserComp.NewWindowRequest
e.Cancel = False
_popupUrl = browserComp.Url.ToString ' WHERE can I get the clicked URL? This is the old one of the remaining window
End Sub
End Class
Code Form2
Public Class frmPopup
End Class
Following popup/new-Window-Create-function works for me:
Private Sub browserComp_NewWindowCreated(ByVal sender As Object, ByVal e As WebKit.NewWindowCreatedEventArgs) Handles browserComp.NewWindowCreated
frmPopup.Text = "POPUP"
Dim popupBrowser As WebKit.WebKitBrowser
popupBrowser = e.WebKitBrowser
frmPopup.Controls.Add(popupBrowser)
frmPopup.Show()
End Sub
whereas frmPopup is a new form.
Before I tried this I already added the Webkit-component to the new form, which might had been the problem. I assume, the trick is, to create a new WebKitBrower-element that is directly connected to the argument e.WebkitBrowser instead of overloading an existing webkitbrowser-component in the form. Don't ask me for reasons for this now (I really don't know) :P
Oh, I should add that I used the Webkit.NET component. The same trick works also for the OpenWebkitSharp-wrapper
The hint-problem still remains ...

How to re-write url in asp.net 3.5

I converted a project from html into aspx
Issue is, all the extension got changed.
e.g. "www.example.com\index.html" Changed to "www.example.com\index.aspx"
which give problem to SEO's.
so now when i search for the web i get the link as www.example.com\index.html and if i try to go in it, it give me the error of 404 file not found.
I tried couple of methods for URL-ReWriting, it works fine at local side, but it fails at server side.
Both i tried in Global.asax
1.
Protected Overloads Sub Application_BeginRequest(ByVal sender As Object, ByVal e As System.EventArgs)
Dim CurrentPath As String = Request.Path.ToLower()
Dim strPageName As String = CurrentPath.Substring(CurrentPath.LastIndexOf("/") + 1, (CurrentPath.LastIndexOf(".") - CurrentPath.LastIndexOf("/")) + 4)
If strPageName.EndsWith(".html") Then
Select Case strPageName
Case "index.html"
RewriteUrl(CurrentPath)
End Select
End If
End Sub
Protected Sub RewriteUrl(ByVal URL As String)
Dim CurrentPath As String = URL
CurrentPath = CurrentPath.Replace(".html", ".aspx")
Dim MyContext As HttpContext = HttpContext.Current
MyContext.RewritePath(CurrentPath)
End Sub
2.
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
RegisterRoutes(RouteTable.Routes)
End Sub
Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.Add("Index", New Route _
( _
"index.html", New CustomRouteHandler("~/index.aspx") _
))
End Sub
Do you have control of the web server? What you most likely need SEO-wise is to create a permanent (301) URL redirect which you can do at the web server level...often in the server settings, or in a script file.
See: http://en.wikipedia.org/wiki/URL_redirection
I realize this doesn't answer your question directly, but if all you are trying to do is get the search results to the correct new page, this is the best way to do it.