Solved

Stop AxWebBrowser navigation on Anchor click

Posted on 2008-06-17
5
767 Views
Last Modified: 2013-12-16
Hello Experts,
I want get the Anchor element href when it is clicked. With the attached code, I could get the href. But I dont want the Axwebbrowser to navigate to the href url. How can achieve thiis.

Thanks a billion


private void Form1_Load(object sender, EventArgs e)
        {
            
            object null_obj_str = "";
            System.Object null_obj = 0;
            axWebBrowser1.Navigate("about:blank", ref null_obj,ref null_obj, ref null_obj_str, ref null_obj_str);
            loadhtml();
        }
 
private void loadhtml()
        {
            IHTMLDocument2 myDoc = new HTMLDocumentClass();
            myDoc = (IHTMLDocument2)axWebBrowser1.Document;
            myDoc.write("<marquee WIDTH=100% BEHAVIOR=SCROLL DIRECTION=left scrollamount=2 BGColor=yellow onmouseover=stop(); onmouseout=start();><a href=http://www.raftaar.com/>akhtar</a> </marquee>");
            myDoc.close();
 
            HTMLDocumentEvents2_Event htmlEvents = (HTMLDocumentEvents2_Event)myDoc;
            htmlEvents.onclick += new HTMLDocumentEvents2_onclickEventHandler(htmlEvents_onclick);
        }
 
private bool htmlEvents_onclick(IHTMLEventObj pEvtObj)
        {
            if (pEvtObj.srcElement.tagName == "A")
            {
                HTMLAnchorElementClass a = pEvtObj.srcElement as HTMLAnchorElementClass;
                textBox1.Text = a.href;
            }
            return true;
        }

Open in new window

0
Comment
Question by:SHAHEEN_AKHTAR
[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
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:joechina
ID: 21815349
Have you tried to cancel the request in navigating event?

0
 

Author Comment

by:SHAHEEN_AKHTAR
ID: 21818511
I have tried onbeforenavigate event in which I called Axwebbrowser.Stop() method. This is also not working.

On downloadBegin event works fine for only first click. On second click, it doesnot work.

Can you kindly explain what you mean please.

Thanks a lot
0
 
LVL 13

Accepted Solution

by:
joechina earned 500 total points
ID: 21822245
Sorry, I am not so familiar with Axwebbrowser control.
.NET has a new WebBrowser control which has a Navigating event. In the event, you can set a flag to cancel a request.
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx

From my experience, the new control could do almost everything as the old ActiveX control except the NewWindow2 event.
0
 

Author Closing Comment

by:SHAHEEN_AKHTAR
ID: 31468229
Thanks dear joechina. That is fine
0
 
LVL 13

Expert Comment

by:joechina
ID: 22008006
Hi, SHAHEEN_AKHTAR

Just let you know the new WebBrowser control is a wrap of the AxWebBrowser control. You can get a instance of the ActiveX control by:

SHDocVw.WebBrowser activeXBrowser = (SHDocVw.WebBrowser)webBrowser1.ActiveXInstance;

You can use all events and properties from both.
0

Featured Post

How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

624 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