How to determine URL about to be opened by the NewWindow event? - vb.net

I'm trying to build a simple desktop application to keep users on a certain website. It's very simple, just a webbrowser control with right click disabled. However certain applications on our website require popup windows and those windows always open up a full browser window.
Private Sub WebBrowser1_NewWindow(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
'What will the URL of this new window be?
Console.WriteLine("New Window maybe to " & WebBrowser1.Url.AbsoluteUri)
End Sub
Catching the newwindow event seems straight forward, but the event does not specify the URL that is being passed to the new window. I have seen some mention of a "NewWindow2" event, but vb.net does not appear to have anything like that.
How can I get the URL that is about to be opened in a new window?
The link is generated from a normal web application and not through a webbrowser.navigate call so it could be just about anything.
Thank you

You can catch the NewWindow2 event, create a new browser instance, host it in a new form or tab and pass it to ppDisp, then handle BeforeNavigate2 to get the new url. You can cancel the navigation if necessary. See Handle the BeforeNavigate2 event in the new window to get the url. for details.

Related

Enable Tab Key in my Web Browser Control

I have a web browser control in a custom task pane control (User Control) and I open it as a sidebar as soon as my Outlook opens (I have created it as an Outlook Addin using Visual Studio 2013). The web browser control has a login form inside it and I would like to place focus on an input control in my web browser control as soon as Outlook opens. I have tried several tricks like placing focus on the custom user control and then placing focus on the input control after the form has loaded but it doesn't place focus on it. Also I have been trying to allow the use of Tab and Delete keys to work inside the web browser control so that I can tab to other controls and play with it like we would do it on a normal browser window. Kindly let me know how I can achieve this as I am out of ideas.
Cheers.
Try to use the Excel WebBrowser Control instead of the System.Windows.Forms WebBrowser; it handles special key forwarding as TAB, DEL, CTRL+V, etc.
For that change the WebBrowser contructor from
new System.Windows.Forms.WebBrowser();
to
new Microsoft.Office.Tools.Excel.Controls.WebBrowser();
You would need to add references to your project:
Project/Add Referernce/Extensions select
Microsoft.Tools.Outlook & Microsoft.Tools.Outlook.v4.0.Utilities
Doc: https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.controls.webbrowser.aspx
You can only do that if you install a Windows hook (SetWindowsHookExW(WH_GETMESSAGE, YourHookProc, 0, GetCurrentThreadId()) and in the hook proc detect that messages are supposed to go to your browser, and redirect them accordingly.
It works partially this way but not 100%. First I had to set the TabStop property to True for my webbrowser control and then just set the focus once the document was loaded and that was it. The tab, delete, backspace keys all ran properly.
Private Sub CustomTaskPane_Load(sender As Object, e As EventArgs) Handles Me.Load
TestWebBrowser.Size = New Drawing.Size(Me.Width, Me.Height)
Me.Focus()
TestWebBrowser.Navigate(url)
WaitForPageLoad()
TestWebBrowser.TabIndex = 0
TestWebBrowser.TabStop = True
End Sub
Private Sub TestWebBrowser_DocumentCompleted(sender As Object, e As Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles TestWebBrowser.DocumentCompleted
' Now set the focus on the Web Browser Control
TestWebBrowser.Focus()
End Sub

Visual Basic - URL showing when click on a link

Just wonder if you could help.
Basically I have a little project to finish, I have created a web browser with VB.NET, when I navigate let's say Google.com, I click to open a link on Google's website and it opens in a new page which is fine but the TextBox on my project (where the URL goes) does not appear any URL to which page/website I've gone to.
Can someone help me on this please?
Thank you so much!
Marco
You can do this by adding a handle to the Navigated event of the WebBrowser.
Private Sub WebBrowser1_Navigated(sender As System.Object, e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated
Me.TextBox1.Text = Me.WebBrowser1.Url.ToString()
End Sub
The WebBrowser.Navigated Event is raised when the control has navigated to a new document and has begun loading it.

WebBrowser control and cookies

I have a problem with WebBrowser control and cookies.
First of all what happens when one navigates in a normal Web browser (IE9 in my case):
1.1. I open Web page http://www.gmail.com.
I enter my username / password,
I leave the checkbox "Stay signed in" unchecked and click "Sign in",
IE9 opens my GMail page with all my mails listed. That is OK.
1.2. At the top of GMail page there are a lot of links like "Calendar", "Documents", etc.
When I click the "Documents" link, my documents page is opened in a separate tab in IE9. No additional login information as name / psw is asked. This is fine too.
Now, what happens when I repeat all that in WebBrowser control (I have created a very simple VB.NET application with single WebBrowser control in it).
2.1. In form load event the following code is executed:
Private Sub MyForm_Load(sender As System.Object, e As System.EventArgs)
Me.MyWebBrowser.Navigate("http://www.gmail.com")
End Sub
2.2. I enter my GMail login information (name and psw) in WebBrowser control,
2.3. When I click the "Documents" link, a new instance of IE9 is opened,
2.4. Instead of showing a list of my documents, Google asks me to login again in an IE9 window. Why? Why I have to enter my credentials again?
I think there is something wrong with cookies and they are not set correctly in a step 2.2. Do you have any ideas what is wrong and what must be done to get cookies set correctly?
Thanks in advance,
Sal
Additional info:
I have my WebBrowser NewWindow event function implemented as:
Private Sub MyWebBrowser_NewWindow(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles MyWebBrowser.NewWindow
Dim CookiesArr As String() = MyWebBrowser.Document.Cookie.Split(";")
For Each Cookie In CookiesArr
Dim Idx As Long = Cookie.IndexOf("=")
If Idx <> -1 Then
Dim CookieName As String = Cookie.Substring(0, Idx).Trim
Dim CookieValue As String = Cookie.Substring(Idx + 1).Trim
InternetSetCookie("http://www.google.com", Nothing, CookieName + " = " + CookieValue + "; expires = Sat,05-Jan-2013 00:00:00 GMT")
End If
Next
End Sub
I believe that InternetSetCookie() method should store cookies permanently in in "C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Cookies" directory for reuse when Google page, requiring authorization, is opened.
that is because the web browser control is opening your link in a separate IE9 window, right? If you open it or have it opened in another web browser control window in your Winforms program, or in the same window where you clicked the link, then it should work properly.
They are using in session cookies (in memory) to hold your login information, not the sort written to the hard drive, this is why when you run another process, the information (in memory session cookies) are not present or propagated to the new process.
So, to intercept link clicking and have it open in a wb window of your choice, you need to intercept the newwindow event, cancel navigation and renavigate using .navigate to the wb of your choice, if you need help with this let me know but there are plenty online.
Also, keep in mind that the web browser control uses IE7 by default, even though you have IE9 installed, this can be changed via the registry.
Try Adding This:
Private Declare Function InternetSetCookie Lib "wininet.dll" Alias "InternetSetCookieA" (ByVal lpszUrlName As String, ByVal lpszCookieName As String, ByVal lpszCookieData As String) As Boolean

Windows Mobile SDK : how to turn a label/button into a website link?

I am programming in Windows Mobile SDK 6 using Visual Basic, i would like to know how to make a button open the smartphone browser with a specific website path (make a link)
I got to the next piece of code so far:
Public Class GuitarHelperPage
Public link As New WebBrowser
Public adress As Uri
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
adress = New Uri("https://www.google.com")
Try
link.Navigate(adress)
'link.Focus()
Catch ex As System.UriFormatException
Return
End Try
End Sub
(the commented 'link.Focus()' its just something i tryed out, but i got the same output)
I simply want to click the button and open the browser at google homepage. But when I click it, the warning "This page contains both secure and nonsecure items. Do you want to continue?" appears and when I click "yes", nothing else hapens.
I've been researching and found this on MSDN help pages:
"The WebBrowser class can only be used in threads set to single thread apartment (STA) mode. To use this class, ensure that your Main method is marked with the STAThreadAttribute attribute"
SO i added this my main method:
<STAThread()> _
Shared Sub Main()
But still i get this error : "Type STAThread is not defined." And can't find a away around, I tried to add this same attribute to a Windows Desktop VB project and it works, maybe there is another way to do it in Mobile?
I am using .Net Framework 3.5 and windows mobile sdk 6.0 for this project
Please help, thank you.
If you want to open the default browser, you don't need a WebBrowser control. Just use the Process class, specifically the Start overload that takes in a ProcessStartInfo. Set the UseShellExecute property of the ProcessStartInfo to true so that it will open the default browser (e.g. if the user installed Opera Mobile, it will use that, not just always force IE).

Change target of links in WebBrowser control

Sometimes when I click on a link, it opens in a new window. Can I prevent this from happening and just load the page in my current window?
This is working for me (so far):
Private Sub browser_NewWindow(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles browser.NewWindow
e.Cancel = True
browser.Navigate(browser.StatusText)
End Sub
Really simple, if the status text is the url (which it always is for the documents I am browsing)
If you are targeting Windows XP SP2 or later, you can catch the NewWindow3 event to get the target url, and after setting the cancel parameter, navigate to the url instead.
For earlier versions of IE you can catch the obsolete NewWindow event with the same handling as documented in Q185538 How To Cause Navigation to Occur in Same WebBrowser Window