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.
Related
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.
When I add www.stackoverflow.com into my RichTextBox and run the program it is shown in blue and as a hyperlink yet when I click it nothing happens. How can I fix this?
Make sure the text property includes a valid url. E.g. http://www.stackoverflow.com/
set the DetectUrls property to true
Write an event handler for the LinkClicked event.
Personally, I wouldn't pass "IExplore.exe" in as a parameter to the Process.Start call as Microsoft advise as this presupposes that it is installed, and is the user's preferred browser. If you just pass the url to process start (as per below) then Windows will do the right thing and fire up the user's preferred browser with the appropriate url.
private void mRichTextBox_LinkClicked (object sender, LinkClickedEventArgs e) {
System.Diagnostics.Process.Start(e.LinkText);
}
RichTextBox class allows you to customize its behavior when user clicks the hyperlink. Add an event handler for the RichTextBox.LinkClicked event
Process p = new Process();
private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
{
p = Process.Start("IExplore.exe", e.LinkText);
}
You should make sure that DetectUrls is set to true. If that doesn't work on its own, you may need to add a handler for the LinkClicked event.
Is yourTextBox.DetectUrls set to true? We may need some more info to provide a better answer.
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.
i am using System.Windows.Controls.Webbrowser class in my C# app.
I want to recognize if the document title changes.
Therefore i listen on the LoadComplete Event and getting the title
public void webBrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
dynamic doc = browser.Document;
setTitle(doc.Title);
}
The problem now is, that the title could change during loading. In this case the LoadCompleted event would not fire.
Next problem is. I have a html application inside which navigates through javascript functions in the application. In case of using these javascript links, the event would not fire, too.
I am looking for an event like OnTitleChange from CHtmlView (c++)
Is there a possibilty to solve my issue?
Thanks for help.
Not supported by the WPF webbrowser.
You can use windows forms interop to show a windows forms webbrowser control then subscribe to the DocumentTitleChanged event.
This is the working code for me:
System.Windows.Forms.WebBrowser browser = new System.Windows.Forms.WebBrowser();
browser.Navigate(URL);
browser.DocumentTitleChanged += new EventHandler(browser_DocumentTitleChanged);
WindowsFormsHost winFormsHost = new WindowsFormsHost();
winFormsHost.Child = browser;
AddChild(winFormsHost);
public void browser_DocumentTitleChanged(object sender, EventArgs e)
{
setTitle(browser.DocumentTitle);
}
When I add www.stackoverflow.com into my RichTextBox and run the program it is shown in blue and as a hyperlink yet when I click it nothing happens. How can I fix this?
Make sure the text property includes a valid url. E.g. http://www.stackoverflow.com/
set the DetectUrls property to true
Write an event handler for the LinkClicked event.
Personally, I wouldn't pass "IExplore.exe" in as a parameter to the Process.Start call as Microsoft advise as this presupposes that it is installed, and is the user's preferred browser. If you just pass the url to process start (as per below) then Windows will do the right thing and fire up the user's preferred browser with the appropriate url.
private void mRichTextBox_LinkClicked (object sender, LinkClickedEventArgs e) {
System.Diagnostics.Process.Start(e.LinkText);
}
RichTextBox class allows you to customize its behavior when user clicks the hyperlink. Add an event handler for the RichTextBox.LinkClicked event
Process p = new Process();
private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
{
p = Process.Start("IExplore.exe", e.LinkText);
}
You should make sure that DetectUrls is set to true. If that doesn't work on its own, you may need to add a handler for the LinkClicked event.
Is yourTextBox.DetectUrls set to true? We may need some more info to provide a better answer.