Mr_Green
Mr_Green

Reputation: 41832

attach a panel to TreeView control

I am beginner in c#. In my project, I populated a xml file inside a TreeView control. If the xml file is large, the TreeView control is showing the data with scroll bars. Beside this, whenever the user double clicks a node I am showing a panel beside the selected node something like this..

enter image description here

When I scroll the TreeView Control :

enter image description here

My question is how to make the panel attached to treeView control so that eventhough the user scrolls the TreeView control the panel should also move along with the selected node.

Upvotes: 0

Views: 1636

Answers (1)

Hans Passant
Hans Passant

Reputation: 941317

Well, hard to do since TreeView doesn't have a Scroll event. It isn't reliable anyway since nodes can be expanded and collapsed, changing the position and visibility of the node. The backup plan is to use a Timer. This worked well:

    private void timer1_Tick(object sender, EventArgs e) {
        var node = treeView1.SelectedNode;
        if (node == null || !node.IsVisible) panel1.Visible = false;
        else {
            panel1.Visible = true;
            var nodepos = treeView1.PointToScreen(node.Bounds.Location);
            var panelpos = panel1.Parent.PointToClient(nodepos);
            panel1.Top = panelpos.Y;
        }
    }

Upvotes: 2

Related Questions