Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4001636
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:55:44+00:00 2026-05-20T07:55:44+00:00

I would like to convert an IHTMLDOMNode to IHTMLElement in C#, I have tried

  • 0

I would like to convert an IHTMLDOMNode to IHTMLElement in C#, I have tried the following:

IHTMLElement tempElement = node as IHTMLElement;
//node is a instance of IHTMLDOMNode internface

However, this did not work — tempElement is null. Is there a way to perform this conversion correctly? Note that in my application, I try to use WebBrowser to access every node in the DOM tree and get their coordinates.

here are my source code, could you tell me what should I do?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mshtml;

namespace TestWindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            webBrowser1.ScrollBarsEnabled = true;
            webBrowser1.Url = new Uri("http://www.bing.com");
            webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
        }

        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            HTMLDocument document = (HTMLDocument)webBrowser1.Document.DomDocument;
            IHTMLDOMNode bodyNode = (IHTMLDOMNode)webBrowser1.Document.Body.DomElement;
            TranversalDOM(document, bodyNode);
        }

        private void TranversalDOM(HTMLDocument document, IHTMLDOMNode node)
        {
            if (node.nodeType == 3)
            {
                string nodeInfo = node.nodeValue.ToString();

                nodeInfo += ">>>";
                IHTMLElement tempElement = node as IHTMLElement;
                //how to convert IHTMLDOMNode to IHTMLElement?

                int X1 = findPosX(tempElement);
                int X2 = X1 + tempElement.offsetWidth;
                int Y1 = findPosY(tempElement);
                int Y2 = Y1 + tempElement.offsetHeight;
                nodeInfo += " LeftTop: (";
                nodeInfo += X1.ToString();
                nodeInfo += ",";
                nodeInfo += Y1.ToString();
                nodeInfo += ")";
                nodeInfo += " RightBottom: (";
                nodeInfo += X2.ToString();
                nodeInfo += ",";
                nodeInfo += Y2.ToString();
                nodeInfo += ")";
                listBox1.Items.Add(nodeInfo);
            }
            else
            {
                IHTMLDOMChildrenCollection childNodes = node.childNodes as IHTMLDOMChildrenCollection;
                foreach (IHTMLDOMNode n in childNodes)
                {
                    TranversalDOM(document, n);
                }
            }
        }


        public int findPosX(IHTMLElement obj)
        {
            int curleft = 0;
            if (obj.offsetParent != null)
            {
                while (obj.offsetParent != null)
                {
                    curleft += obj.offsetLeft;
                    obj = obj.offsetParent;
                }
            }

            return curleft;
        }

        public int findPosY(IHTMLElement obj)
        {
            int curtop = 0;
            if (obj.offsetParent != null)
            {
                while (obj.offsetParent != null)
                {
                    curtop += obj.offsetTop;
                    obj = obj.offsetParent;
                }
            }

            return curtop;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:55:45+00:00Added an answer on May 20, 2026 at 7:55 am

    You cannot convert when it is not an IHTMLElement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i would like convert xml, that: tag is the name of current node (this,
I would like to convert a string into a node. I have a method
I would like to convert the following string to DOM structure. text node <div>div
I would like to convert the following string into an array/nested array and iterate
I would like to convert this piece of logic with purely matrix operations instead
I would like to convert a TBitMap to a PBitMap in KOL. I tried
I would like to convert a yyyy-mm-dd to something like this: Saturday, 2 October
I would like to convert my BitmapImage to a System.Drawing.Image ? This is my
I would like to convert following Query into HQL Query. How can i do?
I would like to convert this /bin/sh syntax into a widely compatible Windows batch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.