Solved

Stop AxWebBrowser navigation on Anchor click

Posted on 2008-06-17
5
756 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
  • 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now