Windows Develop Bookmark and Share   
 index > Windows Forms Designer > OwnerDraw Tooltip in TreeView
 

OwnerDraw Tooltip in TreeView

I am trying to create a custom tooltip for a TreeView control. The tooltip has a draw event and a popup event. During the popup event, the tooltip container is resized to handle the text which is a larger font. The tooltip works fine as long as the mouse is NOT over TreeNode text. However, as soon as the mouse moves over a node, the popup event is not called, and the tooltip is not sized correctly, truncating the text.

Some of the code:

Initializing the tooltip
            // Creating the Custom Tooltip
            m_toolTip = new ToolTip();
            m_toolTip.InitialDelay = 1000;  // duration of mouse hover before tooltip is shown
            m_toolTip.ReshowDelay = 1000;   // duration of mouse hover over 2nd control before tooltip is shown
            m_toolTip.AutoPopDelay = 20000; // duration tool tip is shown
            m_toolTip.OwnerDraw = true;
            m_toolTip.ShowAlways = true;
            m_toolTip.Draw += new DrawToolTipEventHandler(toolTip_Draw);
            m_toolTip.Popup += new PopupEventHandler(toolTip_Popup);

            // TreeView tooltips should not display
            myTreeView1.ShowNodeToolTips = false;


Drawing the custom tooltip

        // Handles drawing the ToolTip.
        private void toolTip_Draw(System.Object sender,
            System.Windows.Forms.DrawToolTipEventArgs e)
        {
            // Draw a custom background and text if the ToolTip is for treeview.
            if (e.AssociatedControl == myTreeView1)
            {
                // Draw the custom background.
                e.Graphics.FillRectangle(SystemBrushes.Info, e.Bounds);

                // Draw the standard border.
                e.DrawBorder();

                // Draw the custom text.
                // The using block will dispose the StringFormat automatically.
                using (StringFormat sf = new StringFormat())
                {
                    sf.Alignment = StringAlignment.Center;
                    sf.LineAlignment = StringAlignment.Center;
                    sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.None;
                    sf.FormatFlags = StringFormatFlags.NoWrap;
                    using (Font f = new Font("Microsoft Sans Serif", 12))
                    {
                        e.Graphics.DrawString(e.ToolTipText, f,
                            SystemBrushes.InfoText, e.Bounds, sf);
                    }
                }
            }
        }

Handling tooltip resize on Popup - This does not get called when mouse is over TreeNode, and text is therefore truncated.

        // Determines the correct size for the treeview ToolTip.
        private void toolTip_Popup(object sender, PopupEventArgs e)
        {
            if (e.AssociatedControl == myTreeView1)
            {
                using (Font f = new Font("Microsoft Sans Serif", 12))
                {
                    // Get tool tip text and add space to make sure the last character does not get trunctated
                    string tipText = m_toolTip.GetToolTip(e.AssociatedControl) + "  ";
                    e.ToolTipSize = TextRenderer.MeasureText(tipText, f);
                }
            }
        }

MouseMove event that sets tooltip (from Microsoft example)

        private void myTreeView1_MouseMove(object sender, MouseEventArgs e)
        {

            // If the node is null, then the mouse is not over a node 
            // and the tool tip is set to be empty
            if (node == null)
            {
                m_toolTip.Hide(myTreeView1);
                m_toolTip.Active = false;
                m_toolTip.SetToolTip(myTreeView1, string.Empty);
            }
            else
            {

                // Set the tool tip to the Text of the node so that if a mouse hover
                // triggers the tool tip, the node's text is displayed.
                string nodeTip = node.Text;
                string currentTip = m_toolTip.GetToolTip(myTreeView1);
                // Only change the tool tip if the node text is populated 
                // and it isn't already set in the tool tip
                if (nodeTip != null)
                {
                    if ((!m_toolTip.Active) || (!nodeTip.Equals(currentTip)))
                    {
                        if (!nodeTip.Equals(currentTip))
                        {
                            m_toolTip.Hide(myTreeView1);
                        }
                        m_toolTip.Active = false;
                        m_toolTip.SetToolTip(myTreeView1, nodeTip);
                        m_toolTip.Active = true;
                    }
                }
            }
        }

If I set ShowNodeToolTips = true, then it seems to temporarily work correctly, but eventually crashes as described in this support document:
http://support.microsoft.com/default.aspx/kb/953102

If there is another way to accomplish custom draw tooltips in a treeview, I would appreciate any help in how to get this working.
crhsAdmin  Monday, June 15, 2009 9:42 PM
Hi crhsAdmin,

I have tested your code. The result is as what you said, the ToolTip control have some problem with TreeView control. The Popup event doesn't occur when the mouse point is over the node. In TreeView, there also be ToolTip for each node, when you use ToolTip for TreeView, it may cause the problem.

Here is an article introducing a powerful popup control, wish you can get some help from it.
http://www.codeproject.com/KB/miscctrl/simplepopup.aspx

This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Thursday, June 18, 2009 5:33 AM
Hi crhsAdmin,

I have tested your code. The result is as what you said, the ToolTip control have some problem with TreeView control. The Popup event doesn't occur when the mouse point is over the node. In TreeView, there also be ToolTip for each node, when you use ToolTip for TreeView, it may cause the problem.

Here is an article introducing a powerful popup control, wish you can get some help from it.
http://www.codeproject.com/KB/miscctrl/simplepopup.aspx

This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Thursday, June 18, 2009 5:33 AM
Thank you for the response and validating that this is a problem. The popup control looks promising, but lacks a delay feature, so it would need some work to be usable as a tooltip.
crhsAdmin  Friday, June 19, 2009 2:07 PM

You can use google to search for other answers

Custom Search

More Threads

• Proper way to set parent of control dropped on a custom root designer?
• Design time localization for custom controls
• Possible Bug in VS 2008
• Category property, System.ComponentModel.CategoryAttribute
• Embedded control problem
• About combo box.
• HOWTO avoid that the IDE place the propertys ordered alphabetically ?
• Problems with the "(Name)" Property when I implements IExtenderListService
• Failed to parse method 'InitializeComponent' error after upgrade from 2003 to 2005
• Adding a combobox to the statusbar ...