Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

WPF browser control link opening

Posted on 2014-01-14
10
Medium Priority
?
423 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

783 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