Solved

WPF browser control link opening

Posted on 2014-01-14
10
385 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
New to SOAP and need some direction 10 45
About delegates in c sharp 3 35
Reset asp.net login password 4 29
Automated testing suggestions? 2 26
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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

910 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

25 Experts available now in Live!

Get 1:1 Help Now