(C#) How can I find the screen location of a treeview item?

Posted on 2011-10-18
Last Modified: 2012-05-12
The location changes, because the treeview is scrolling. I am drawing an additional bitmap next to a highlighted item. If I could find the coordinates of a given item, I'd have no problem drawing that bitmap in the required location.

Thanks anyone who can help!
Question by:rustymoling
    LVL 35

    Expert Comment

    by:Miguel Oz
    If you are using winforms, check mouse move event:

    private void MouseMoveHandler(object sender, MouseEventArgs e)
        // Get the x and y coordinates of the mouse pointer.
        Point position = e.GetPosition(this);
        double pX = position.X;
        double pY = position.Y;

        // Asuming your treeview width is 100 and scrollbar width is 10.
        position.Y = 100-2*10;//position.Y;
     // Convert to a point in the TreeView's coordinate system. (these are the coordinates you need)
                position  = treeview1.PointToClient(position );
                // Get the node underneath the mouse if required to check if piicking the right node.
                TreeNode node = treeview1.GetNodeAt(pt);


    Author Comment

    Well, you are getting coordinates from the event args of a Mouse Move event - and I don't want to use that. Things are changing in my tree view as a result of a background process. And there are more things in the treeview than can fit on the screen, so the scrollbar is active. And users minimize and maximize this window, which sometimes strangely alters what list of items is visible on the screen.

    To me, it seems simplest to locate which treenode is the currently relevant one, and get hold of its current screen position, but I haven't had any luck with that - I don't know which properties it might have to help me with that, and I don't know how to cast it to anything that might have the properties I'm looking for.
    LVL 35

    Accepted Solution

    Use bounds property, The node coordinates are relative to the upper-left corner of the TreeView control.

    Author Closing Comment

    Exactly what I needed, thank you.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    This video discusses moving either the default database or any database to a new volume.

    733 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

    17 Experts available now in Live!

    Get 1:1 Help Now