Solved

WPF browser control link opening

Posted on 2014-01-14
10
383 Views
Last Modified: 2014-01-16
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
0
Comment
Question by:deanlee17
  • 5
  • 5
10 Comments
 
LVL 3

Expert Comment

by:fredvr666
ID: 39779411
I used it like this:
this.WebBrowser1.Navigate("http://www.google.com");
0
 

Author Comment

by:deanlee17
ID: 39779448
Hi, how does that capture links clicked on a page?
0
 
LVL 3

Expert Comment

by:fredvr666
ID: 39779581
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
0
 

Author Comment

by:deanlee17
ID: 39782187
Ok I got the attached error:
error.png
0
 
LVL 3

Expert Comment

by:fredvr666
ID: 39782376
rename the WebBrowser1_LoadCompleted
to WebBrowser_Sourcing_Netcomponents_Enquiryebrowser_LoadCompleted
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:deanlee17
ID: 39782464
Attached.

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

Thanks
error.png
0
 
LVL 3

Expert Comment

by:fredvr666
ID: 39782571
put the navigate after the loadcompleted init
0
 

Author Comment

by:deanlee17
ID: 39782629
Put it on a button command, still no joy there.
0
 
LVL 3

Accepted Solution

by:
fredvr666 earned 500 total points
ID: 39784668
This is the code I used: ( and this worked)

public MainWindow()
        {
            InitializeComponent();
            this.WebBrowser_Sourcing_Netcomponents_Enquiry.LoadCompleted += new LoadCompletedEventHandler(WebBrowser_Sourcing_Netcomponents_Enquiry_LoadCompleted);
            OpenBrowser();
        }

        private void OpenBrowser()
        {
            this.WebBrowser_Sourcing_Netcomponents_Enquiry.Navigate("https://www.brokerforum.com");
        }



        void WebBrowser_Sourcing_Netcomponents_Enquiry_Navigated(object sender, NavigationEventArgs e)
        {
           
        }

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

Author Comment

by:deanlee17
ID: 39785408
Perfect, thanks.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now