OK, I took some time and got the complete solution for you. It was easy and worked from the first attempt!
You just ignored some of my words. Did I tell you to use another event and cancel redirection to "href="https://docs.google.com/viewer?url=..."? Also, pay attention: this URL is HTTPS.
Now, I'll show how. This is a complete solution, all in code, without the use of the designer, to show it all in one file:
using System.Windows.Forms;
public class MyForm : Form {
const string viewer = "https://docs.google.com/viewer";
const string viewerUrl = viewer + "?url={0}";
const string docSample = "https://docs.google.com/viewer?url=http://www.gotw.ca/publications/C%2B%2BCLIRationale.pdf";
WebBrowser browser = new WebBrowser();
static bool IsDocumentToRedirect(string url) {
return url.ToLower().EndsWith(".pdf");
}
public MyForm() {
browser.Dock = DockStyle.Fill;
browser.Parent = this;
browser.Navigating += (sender, eventArgs) => {
string urlString = eventArgs.ToString();
if (IsDocumentToRedirect(urlString) && !urlString.ToLower().StartsWith(viewer)) {
eventArgs.Cancel = true;
browser.Navigate(string.Format("{0},{1}", eventArgs.Url));
}
};
}
protected override void OnShown(EventArgs e) {
this.browser.Navigate(docSample);
}
}
Please note that I uses
System.Windows.Forms.WebBrowser
. I simplified down the code, to show only one test, on form shown.
I hope you got the idea. The key it to handle the event when the navigation is only started and can be cancelled.
I hope you got the idea and can see what was missing from your code (your previous question).
Good luck,
—SA