Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a link in webBrowser control in external browser?

Tags:

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?

like image 542
Shiva Pareek Avatar asked Aug 03 '13 17:08

Shiva Pareek


People also ask

How to cancel webbrowser navigation?

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; }

How to force links to open in Microsoft Edge browser?

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.

Why can't I access the URL or element that was clicked?

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.

How to show compiled form in textbox and webbrowser?

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; }


2 Answers

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());
}
like image 175
keyboardP Avatar answered Sep 19 '22 02:09

keyboardP


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;
        }
    }
like image 40
Trikaldarshiii Avatar answered Sep 19 '22 02:09

Trikaldarshiii