Monday, February 21, 2011

How to change ToolTip's position on a TreeView?

When using tooltips to show a detailed description of a TreeNode, the tooltip is drawn on top of the node, as if it was completing the node's text. Also, if the text is long, the tooltip is positioned in a way that the text exceeds the screen.

But what I need is the tooltip to show right below the mouse pointer and not on top of the TreeNode.

Any idea how to do this?


Show, don't tell:

How it is:

how it is

How I want:

how I want

From stackoverflow
  • You need to define a ToolTip and write an MouseOverEventHandler for the TreeView. In the MouseOverEventHandler calculate the node above which mouse is positioned, then show the description ToolTip. Also make sure you are not setting the tooltip description more than once, otherwise the behavior is quite ugly.

    A better way is to show the description in the StatusStrip - bottom left of the Form.

    Update:

    OK since you have clarified your question. You can use ToolTip.Show method where you can specify coordinates:

    public void Show(
        string text,
        IWin32Window window,
        int x,
        int y,
        int duration
    )
    

    Obviously, you'll have to add offset to x and y.

    Jonas : Not really what I want. Actually I already do this (setting the tooltip text on MonseMove for each node). What I need is to prevent the tooltip form showing on top of the node, as if it was completing the node's text.
  • I didn't find the answer I was looking for, but I somehow made it work the way I wanted.

    Before, I was trying to set up the tooltip as follows:

        private void treeView1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            TreeNode node = treeView1.GetNodeAt(e.X, e.Y);
            if (node != null)
            {
                    string text = GetNodeTooltip(node);
                    string currentText = toolTip1.GetToolTip(treeView1);
    
                    if (text.Equals(currentText) == false)
                    {
                        toolTip1.SetToolTip(treeView1, text);
                    }
                }
                else
                {
                    toolTip1.SetToolTip(tree, string.Empty);
                }
            }
            else
            {
                toolTip1.SetToolTip(tree, string.Empty);
            }
        }
    

    Now, I just make treeView1.ShowNodeToolTips=true and when I create every node, I just set its TreeNode.ToolTipText value with the desired text.

  • private ToolTip toolTipController = new ToolTip() { UseFading = false,UseAnimation = false};

    protected override void OnMouseMove(MouseEventArgs e) { var node = GetNodeAt(e.X, e.Y); if (node != null) { var text = node.Text;

                if (!text.Equals(toolTipController.GetToolTip(this)))
                {
                    toolTipController.Show(text, this, e.Location, 2000);
                }
            }
            else
            {
                toolTipController.RemoveAll();
            }
        }
    

0 comments:

Post a Comment