Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to show the clicked link of the browser in a label. Every time the user click on a link, the link is shown in the label, exept when i click on a *.doc, the value of the label dont change and nothing happen! I know It will be much easy with the default browser, i changed to web kit because it was ver laggy and it freeze the whole application when the user is surfing. can someone help me with this please??

C#
private void webKitBrowser1_Navigated(object sender, WebKitBrowserNavigatedEventArgs e)
    {
        label1.Text = Convert.ToString(webKitBrowser1.Url);
    }


it seems that when i click on a .doc link nothin happen, like if i didn't click anywhere.. like if the .doc click is disabled in webKitBrowser and we should enable it
Posted
Updated 10-May-13 12:09pm
v3

This is because you don't really navigate to *.DOC. You need to start using the debugger, by the way.

—SA
 
Share this answer
 
Comments
Nnorss 10-May-13 17:34pm    
so i navigate to where? how can i use debugger?
Nnorss 10-May-13 17:40pm    
thanks for informing me about the debugger! but where do i navigate?
Nnorss 10-May-13 18:09pm    
i placed a stop point... it seems that when i click on a .doc link nothin happen, like if i didn't click anywhere.. like if the .doc click is disabled in webKitBrowser and we should enable it
Nnorss 10-May-13 18:33pm    
5 stars but it's not a solution :p
Sergey Alexandrovich Kryukov 10-May-13 19:26pm    
Thank you, I know... :-)
At least I explain you the problem. You should come back to your previous post. Also, there is a possibility to bite a bulled and develop presentation of Word document as HTML, instead of Google...
—SA
OK, I took some time and got the complete solution for you. It was easy and worked from the first attempt!

You just ignored some of my words. Did I tell you to use another event and cancel redirection to "href="https://docs.google.com/viewer?url=..."? Also, pay attention: this URL is HTTPS.

Now, I'll show how. This is a complete solution, all in code, without the use of the designer, to show it all in one file:
C#
using System.Windows.Forms;

//...

public class MyForm : Form {

    const string viewer = "https://docs.google.com/viewer";
    const string viewerUrl = viewer + "?url={0}";
    const string docSample = "https://docs.google.com/viewer?url=http://www.gotw.ca/publications/C%2B%2BCLIRationale.pdf";

    WebBrowser browser = new WebBrowser(); // you probably do it in designer, but I want to show all in 1 file

    static bool IsDocumentToRedirect(string url) {
        return url.ToLower().EndsWith(".pdf");
        // you can also use .doc and .docx, I did PDF for my testing
    } //IsDocumentToRedirect

    public MyForm() {
        browser.Dock = DockStyle.Fill; // would be done in designer
        browser.Parent = this; // would be done in designer
        // commented out, to show that the designer is not used, 3 lines of code replace it:
        // InitializeComponent()
        browser.Navigating += (sender, eventArgs) => {
            string urlString = eventArgs.ToString();
            if (IsDocumentToRedirect(urlString) && !urlString.ToLower().StartsWith(viewer)) {
                eventArgs.Cancel = true;
                browser.Navigate(string.Format("{0},{1}", eventArgs.Url));
            } //if
        }; //browser.Navigating
    } //MyForm

    protected override void OnShown(EventArgs e) {
        // this is done just to simulate the click on anchor and test
        // redirection:
        this.browser.Navigate(docSample);
    } //OnShown

} //class MyForm


Please note that I uses System.Windows.Forms.WebBrowser. I simplified down the code, to show only one test, on form shown.
I hope you got the idea. The key it to handle the event when the navigation is only started and can be cancelled.

I hope you got the idea and can see what was missing from your code (your previous question).

Good luck,
—SA
 
Share this answer
 
v2
Comments
Nnorss 11-May-13 12:39pm    
this work perfectly with webKitBrowser and .doc! infinite thanks and big respect!
Nnorss 11-May-13 15:43pm    
another thing to do is getting the right url, for exemple when i search on google for a doc filetype, it gives me an url lick that: {https://www.google.com/url?q=http://www.website.com/TRAQUEOS.doc&sa=U&ei=_Ye3yAfBQ&ved=0CBgAA&usg=AFXq9jj-5OX}, and unfortunatelly this type is not included in the toRedirect bool!..and also sometime there is empty spaces inside the link...
Sergey Alexandrovich Kryukov 11-May-13 23:02pm    
I described it in my answer to your previous question; it's really easy. It's like have an array { ".doc", ".docx" } (low-case) and iterate to check if there is one match with the actual URL...
—SA
Sergey Alexandrovich Kryukov 11-May-13 23:04pm    
You are very welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900