Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

WPF browser control link opening

Posted on 2014-01-14
10
Medium Priority
?
415 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 

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
 

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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

722 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