Reputation: 257
I have a WebBrowser control in a form.
I want that when the user will click on a link (href with mailto) it will register to the website that the button was clicked, but it will not open a new window (neither Outlook nor any other website).
I have found this code, but it doesn't work:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
TextReader tr = File.OpenText(webBrowser1.Url.ToString());
string htmlFile = tr.ReadToEnd();
tr.Close();
tr.Dispose();
if (htmlFile.Contains("mailto:"))
{
htmlFile = htmlFile.Replace("mailto:", @"mail");
//Recreate new file with fixed html
File.Delete(e.Url.LocalPath);
TextWriter tw = File.CreateText(e.Url.LocalPath);
tw.Write(htmlFile);
tw.Flush();
tw.Close();
tw.Dispose();
Refresh();
}
}
The answer doesn't have to be how to fix this code, if there's a simpler way to do it? It will be better.
Upvotes: 1
Views: 375
Reputation: 2385
this apply to WP8 webbrowser but probably apply to your case too. You register to the navigating event. That event is cancelable so if when you handle the event set e.Cancel=true , it prevent the navigation
private void OnNavigating(object sender, NavigatingEventArgs e)
{
//take the uri string, not sure is the right method name
string uri = e.Uri.AbsoluteUri;
if (uri.StartsWith("mailto"))
e.Cancel = true;
}
Upvotes: 2