Solved

WPF browser control link opening

Posted on 2014-01-14
10
408 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

636 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