Open new page in standard browser when link is clicked - c#

I can open the link in my standard browser with this code:
public void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
//cancel the current event
e.Cancel = true;
//this opens the URL in the user's default browser
Process.Start(e.Url.ToString());
}
But the problem is that IE only should be opened when a link on the webbrowser is clicked. When using this code IE also opens when I change the documenttext.

My suggestion would be to take a different approach. At the point in time immediately after the initial page has loaded in the WebBrowser control (Navigated event), you can use the webBrowser1.Document property to retrieve an HtmlDocument instance.
From this you should be able to find your link by using, for example,
http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementbyid(v=vs.110).aspx
Then you can add an event handler to detect when this link is clicked, and in this handler, run your code to start the IE process.

Related

Click button after webBrowser1_DocumentCompleted event

I have a C# 4.0 WinForms application, which has a WebBrowser control and 2-buttons.
Clicking the first button sends a URL to the browser to navigate to a specified webSite.
Clicking the second button parses the OuterHtml of the webBrowser1.Document, looking for an "https://..." link for File Download.
The code then uses a webClient.DownloadFileAsync to pull down a file for further use in the application.
The above code successfully works, if I manually click those buttons.
In an effort to automate this for the end-user, I place the first button's click event, i.e. btnDisplayWeb.PerformClick(); in the form's Form1_Load event. This also works, allowing the webBrowser1 to populate its Document with the desired webSite.
However, I am unable to programatically click the 2nd button to acquire the web link for file download.
I have tried to place the 2nd buttons click event within the browser's DocumentCompleted event, as shown below.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
btnMyUrl.PerformClick();
}
However, from what I've read on StackOverFlow and other sites, it's possible that this particular event gets called more than once, and hence it fails.
I've also attempted to loop for a number of seconds, or even use a Thread.Sleep(xxxx), but the browser window fails to populate until the sleep or timer stops.
I attempted to use the suggestions found on the following StackOverFlow site shown below.
How to use WebBrowser control DocumentCompleted event in C#?
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string url = e.Url.ToString();
if (!(url.StartsWith("http://") || url.StartsWith("https://")))
{
// in AJAX
}
if (e.Url.AbsolutePath != this.webBrowser.Url.AbsolutePath)
{
// IFRAME
}
else
{
// REAL DOCUMENT COMPLETE
}
}
However, in parsing the OuterHtml, nothing is returned in the first two sections, and in the third section, other elements are returned instead of the desired "https://..." link for File Download.
Interestingly, if I use a webBrowser1.ReadyState event, as shown below, and place a MessageBox inside DocumentCompleted, this seems to allow the browser document to complete, because after clicking the OK button, the parsing is successful.
if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
MessageBox.Show("waiting", "CHECKING");
btnMyUrl.PerformClick();
}
However, I then have the difficulty of finding a way to click the OK button of the MessageBox.
Is there another event that occurs after the DocumentCompleted event.
OR, can someone suggest how to programmatically close the MessageBox?
If this can be done in code, then I can perform the buttonClick() of the 2nd button in that section of code.
After finding that the addition of a MessageBox allows the webBrowser1.Document to complete, and using webBrowser1.ReadyState event within the webBrowser_DocumentCompleted event, all I needed to do, was to find a way to programmatically close the MessageBox.
Further searching on StackOverFlow revealed the following solution on the site below.
Close a MessageBox after several seconds
Implementing the AutoClosingMessageBox, and setting a time interval, closed the MessageBox and allowed my button click, i.e. btnMyUrl.PerformClick(); to successfully parse the OuterHtml and now the code works properly.
Hopefully, if someone else discovers that placing a MessageBox within the webBrowser_DocumentCompleted event allows the document to complete; the aforementioned AutoClosingMessageBox will assist them as well.

WebBrowser opens link in IE instead of Default Browser?

So I have a WPF application that opens up a new window. Then on that new window, it creates a webBrowser object:
WebBrowser browser = new WebBrowser();
browser.Source = new System.Uri(chatUrl);
browser.Navigating += new NavigatingCancelEventHandler(browser_Navigating);
this.browserControl.Child = browser;
As you can see, I have created a hook for NavigatingCancelEventHandler. Based on what I've seen, this handler is supposed to intercept links clicked within the webbrowser.
private void browser_Navigating(object sender, NavigatingCancelEventArgs e)
{
e.Cancel = true;
System.Diagnostics.Process.Start(e.Uri.ToString());
}
So I take the navigation, cancel it and use the Process.Start function to open it in my default browser. The problem is, it doesn't do this. It still opens up in IE9. I've seen other threads here on StackOverflow, and they all say to do what I'm doing. But what I'm doing doesn't work. Please help.
You're using the incorrect delegate type for your event handler, you're specifying a NavigatingCancelEventArgs but it should be a WebBrowserNavigatingEventHandler.
Easiest way to get the right one is to just type
browser.Navigating +=
And press TAB twice, it'll generate the correct handler for you.
Edit: Hmm, I'm looking at the S.W.F version, but i'd try the above in either case for WPF.

Open YouTube Embedded Player link in default browser

I'm hosting YouTube Embedded Player in C# WebBrowser control. When I click button "Watch on YouTube" IE opens. Is there any way to open the link in default web browser, for instance, Chrome?
Like this for example:
private void browser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
System.Diagnostics.Process.Start(e.Url.ToString());
e.Cancel = true;
}
Explanation: change the navigating handler to process the URL and fire the link in the diagnostics (which will open the URL in the user’s default browser).

C# detect postback in webbrowser control

I've created an application that grabs data from a web page and saves them to a database. This pages sends some __postbacks. After the postback the page loads some data that I need to save. I do not know how to detect this event so I've created a button which grabs the data when pressed by the user.
How can I automate the process so that the user won't have to check for the postback himself?
That is how can I detect the postback event in my webbrowser control?
The webbrowser control has a DocumentCompleted event. You can use the WebBrowser.Document property to look for something specific in this event.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
if (webBrowser1.Document.Body.InnerHtml.Contains("some way that I know I am ready"))
}

click on a link event webbrowser c#?

I want to open a link in IE browser from the web browser control in my winform ... the webbrowser control navigate to a page when the user click on a link it opens in the internet explorer browser.
Basically handle the WebBrowser.Navigating Event, cancel the navigation and open up the url in explorer like this:
private void WebBrowserNavigating(object sender, WebBrowserNavigatingEventArgs e)
{
e.Cancel = true;
Process.Start("iexplorer.exe", e.Url);
}

Categories