I have a textBox and a webBrowser control in my Windows Forms application. Whenever a user enters a HTML code in textBox, the webBrowser control shows its compiled form. The code for this:
private void textBox2_TextChanged(object sender, EventArgs e)
{
webBrowser1.DocumentText = textBox2.Text;
}
But whenever I click a link in the webBrowser control, it opens it in the same webBrowser control. What I want is that it should open in default web browser of the system. So is there any event for this webBrowser control that handles link clicking?
Hope this helps!! Process.Start will open the URL in the default browser, and then you just tell the WebBrowser control to cancel navigation. private void webBrowser1_Navigating (object sender, WebBrowserNavigatingEventArgs e) { Process.Start (e.Url.ToString ()); e.Cancel = true; }
To see for yourself how to force links to open in Microsoft's Edge Browser, insert a normal <A> (anchor tag) in the page source, like so: Now, place microsoft-edge: before the hypertext reference link: Shown below is a screenshot of a page containing one of the special "microsoft-edge" links viewed with Firefox.
Because when a user clicks on a link in your web browser control, it doesn't fire the navigating event, it fires the new window event. In the new window event, you do not have access to the url or element that was clicked to interrupt the new window and open a default one. Here is how you do it.
I have a textBox and a webBrowser control in my Windows Forms application. Whenever a user enters a HTML code in textBox, the webBrowser control shows its compiled form. The code for this: private void textBox2_TextChanged (object sender, EventArgs e) { webBrowser1.DocumentText = textBox2.Text; }
The easiest way to do this would be to intercept the Navigating event.
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());
}
I would like to add something more to this answer,
Coz webBrowser1_Navigating method is executed everytime when the content of the webBrowser is changed.
In your case whenever you set the values to DocumentText this method is called and when there is no url and its default value is about:blank. So we should also check this for otherwise it won't load any content.
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if (!(e.Url.ToString().Equals("about:blank", StringComparison.InvariantCultureIgnoreCase)))
{
System.Diagnostics.Process.Start(e.Url.ToString());
e.Cancel = true;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With