Avatar of deanlee17
deanlee17
 asked on

WPF browser control link opening

Hi guys,

I have a WPF app with the browser control dropped onto a tab. All works well until the website being displayed wants to open a link in a new window, this then breaks out of my app and opens a browser window. How can I open all links within the browser control inside my app?

Found the below code and gave it a try but no joy, anyone got a simple solution?

dynamic browser = sender;
            dynamic activeElement = browser.Document.activeElement;
            var link = activeElement.ToString();
            // this is new Window,if you want open in current page,only change the browser.Source and Cancel the event
            WebBrowser_Sourcing_Netcomponents_Enquiry.Source = new Uri(link);

            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.ToString());
            }
            finally
            {
                e.Cancel = true;
            }

Open in new window


Cheers,
Dean
.NET ProgrammingC#

Avatar of undefined
Last Comment
deanlee17

8/22/2022 - Mon
fredvr666

I used it like this:
this.WebBrowser1.Navigate("http://www.google.com");
deanlee17

ASKER
Hi, how does that capture links clicked on a page?
fredvr666

You right I tried this in a project:
add Reference|Extensions: Microsoft.mshtml
In the MainWindow add
WebBrowser1.LoadCompleted += new LoadCompletedEventHandler(WebBrowser1_LoadCompleted);

 void WebBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
        {
            //Ricci Gian Maria
            mshtml.HTMLDocument HTMLdoc = WebBrowser1.Document as mshtml.HTMLDocument;
            foreach (mshtml.IHTMLElement item in HTMLdoc.links)
            {
                var targetAttributeValue = item.getAttribute("target");
                if (targetAttributeValue != null)
                {
                    item.setAttribute("target", "_self");
                }
            }
        }

I tried it this is working
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
deanlee17

ASKER
Ok I got the attached error:
error.png
fredvr666

rename the WebBrowser1_LoadCompleted
to WebBrowser_Sourcing_Netcomponents_Enquiryebrowser_LoadCompleted
deanlee17

ASKER
Attached.

My program still runs, but the web control isn't even visible.

Thanks
error.png
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
fredvr666

put the navigate after the loadcompleted init
deanlee17

ASKER
Put it on a button command, still no joy there.
ASKER CERTIFIED SOLUTION
fredvr666

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
deanlee17

ASKER
Perfect, thanks.
Your help has saved me hundreds of hours of internet surfing.
fblack61