Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Navigate to Anchors in WebBrowser control

We have a web browser in our Winforms app to nicely display history of a selected item rendered by xslt.

The xslt is writing out <a> tags in the outputted html to allow the webBrowser control to navigate to the selected history entry.

As we are not 'navigating' to the html in the strict web sense, rather setting the html by the DocumentText, I can't 'navigate' to desired anchors with a #AnchorName, as the webBrowser's Url is null (edit: actually on completion it is about:blank).

How can I dynamically navigate to Anchor tags in the html of the Web Browser control in this case?

EDIT:

Thanks sdolphion for the tip, this is the eventual code I used

void _history_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        _completed = true;
        if (!string.IsNullOrEmpty(_requestedAnchor))
        {
            JumpToRequestedAnchor();
            return;
        }
    }

    private void JumpToRequestedAnchor()
    {
        HtmlElementCollection elements = _history.Document.GetElementsByTagName("A");
        foreach (HtmlElement element in elements)
        {
            if (element.GetAttribute("Name") == _requestedAnchor)
            {
                element.ScrollIntoView(true);
                return;
            }
        }
    }
like image 557
johnc Avatar asked Sep 18 '09 00:09

johnc


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

I am sure someone has a better way of doing this but here is what I used to accomplish this task.

HtmlElementCollection elements = this.webBrowser.Document.Body.All;
foreach(HtmlElement element in elements){
   string nameAttribute = element.GetAttribute("Name");
   if(!string.IsNullOrEmpty(nameAttribute) && nameAttribute == section){
      element.ScrollIntoView(true);
      break;
   }
}
like image 194
sdolphin Avatar answered Oct 21 '22 03:10

sdolphin


I know this question is old and has a great answer, but this hasn't been suggested yet, so it might be useful for others that come here looking for an answer.

Another way to do it is use the element id in the HTML.

<p id="section1">This is a test section</p>

Then you can use

HtmlElement sectionAnchor = webBrowserPreview.Document.GetElementById("section1");
if (sectionAnchor != null)
{
    sectionAnchor.ScrollIntoView(true);
}

where webBrowserPreview is your WebBrowser control.

Alternatively, sectionAnchor.ScrollIntoView(false) will only bring the element on screen instead of aligning it with the top of the page

like image 33
dbooher Avatar answered Oct 21 '22 05:10

dbooher