locked
How to apply ToolTip to UserControl including SubControls RRS feed

  • Question

  • Hi,

    I've got an UserControl which is added to other Forms and Controls. When those parent Forms or Controls contain a ToolTip Assingnment for that UserControl, the ToolTip is only shown when the mouse hovers on the UserControl background, but not when the mouse hovers over a Control contained in that UserControl.

    Is there any way to fix this issue?

    (Using Windows Forms in .NET 4.0)

    Thanks.

    Tuesday, October 4, 2011 1:26 PM

Answers

  • Hi Markus,

    This is the limitation of toolTip1 for composite custom control. The Tooltip works in this way when being used wi th UserControl. Also thanks for your suggestion. I'd recommend that you forward the recommendation to the Microsoft Wish Program

    But right now, the workaround is to set tooltip for each of the controls for the RepositoryControl in its Load event :

    toolTip1.SetToolTip(this, _Textbox1.Text);

    Sincerely,


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by MarkusSchaber Monday, December 5, 2011 8:29 AM
    Monday, October 31, 2011 9:11 AM

All replies

  • Hello MarkusSchaber,

    What userControl are you using? Would you please give us an example or demo that can reproduce the problem, or it's really hard to figer out what's going wrong from your discription without seeing any code.

    Looking forward to your update.

    Best wishes,


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, October 5, 2011 6:41 AM
  • Hi, Helen,

    My RepositoryControl directly derives from System.Windows.Forms.UserControl. It was created using the Designer, and contains a Button, a Label and a ComboBox, and some event handlers and initialization functions (fill the ComboBox with values, and the button clears the ComboBox Selection and Text).

    Now I have a different UserControl contained in some form. That other UserControls contains several other controls including the RepositoryControl, and a tooltip. I set the Tooltip for the RepositoryControl.

    Now, when my mouse hovers over the RepositoryControl, the tooltip only shows when it is hovering over those places where the RepositoryControl Background is visibile, and not when it is hovering over the Label, Button or ComboBox contained in the RepositoryControl.

    I hope, it is clear enough now. If not, I'll try to create a small self-contained example.

     

     

     

    Wednesday, October 5, 2011 7:20 AM
  • Yes, it is clear, but I don't have the specially usercontrol, so have you tested with a normal container that can still have this problem? If not, so the problem is dedicated with your special usercontrol, would you please provide a demo that help us to reproduce it?
    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 10, 2011 2:19 AM
  • Hello MarkusSchaber,

    How's the issue going on? Was it been solved yet? Or do you get an reproducable demo? If it is solved, would you please share your solution with us?

    Sincerely,


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, October 13, 2011 8:03 AM
  • Hi, Helen,

    It's not solved yet, I just was busy with higher priorty things.

    I now klicked together a small self-contained demo in a few minutes.

    What's the best way to share that demo with you? It seems that I cannot attach zip files in this forum...

    Markus



    Thursday, October 13, 2011 11:49 AM
  • Hi MarkusSchaber,

    You can zip it and upload it to skydrive.com or some free file hosting service.


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 17, 2011 7:27 AM
  • Hi, Helen!

    The link is: https://skydrive.live.com/redir.aspx?cid=ee5b807fa15ec259&resid=EE5B807FA15EC259!249.

    To help illustrating the problem, I configured the UserControl to have a dark blue background:

    So, now if I hover the mouse over the UserControl, the tooltip only works in the blue area (the background of the UserControl), but not over the label, textbox or button which are part of the UserControl.

    Is there any option to make the Tooltip work for the complete area covered by the UserControl?

    Thanks!



    Monday, October 17, 2011 1:36 PM
  • Hi, Helen:

    Have you been able take a look at it yet?

    Thanks!

    Friday, October 21, 2011 11:31 AM
  • Hi Markus,

    This is the limitation of toolTip1 for composite custom control. The Tooltip works in this way when being used wi th UserControl. Also thanks for your suggestion. I'd recommend that you forward the recommendation to the Microsoft Wish Program

    But right now, the workaround is to set tooltip for each of the controls for the RepositoryControl in its Load event :

    toolTip1.SetToolTip(this, _Textbox1.Text);

    Sincerely,


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by MarkusSchaber Monday, December 5, 2011 8:29 AM
    Monday, October 31, 2011 9:11 AM
  • Hi, Helen,

    Setting them in the OnLoad method of the UserControl itsself removes the ability to set the tooltip from the Designer of the containing Control. And this UserControl is to be embedded into different Dialogs with different ToolTips.

    Maybe I add a string property which it uses to set the tooltips...

    Thanks!

    Monday, November 7, 2011 8:06 AM
  • If you know that only one level of depth is enough for your UserControl, you can use this extension method for ToolTip:

     

    namespace FormUtilities
    {
        public static class Tooltip2
        {
            /// <summary>
            /// SetToolTipAll() is an extension method found in FormUtilities. It is designed to
            /// set the same tooltip text for all subcontrols of a given control, rather than
            /// simply for the value passed as a parameter. This is useful when the Control
            /// passed is actually a container control like a UserControl. Normally, only the
            /// area of the UserControl not occupied by any other Control instance would cause
            /// the tooltip to pop up. When using this method to set the tip, all subcontrols
            /// will pop up the tip, too.
            /// </summary>
            /// <param name="tooltip">
            /// ToolTip instance for which the tip will be set for the given control.
            /// </param>
            /// <param name="control">
            /// Control instance for which the tooltip will be set. If the Control is an 
            /// instance of a container type, all subcontrols will also be set for the same
            /// tooltip.
            /// </param>
            /// <param name="text">
            /// String value for the tooltip text.
            /// </param>
            public static void SetToolTipAll(this ToolTip tooltip, Control control, string text)
            {
                tooltip.SetToolTip(control, text);
    
                foreach (Control c in control.Controls)
                {
                    tooltip.SetToolTip(c, text);
                }
            }
        }
    }
    
    


    The idea here is that you add a new SetToolTip-type method to ToolTip called SetToolTipAll. This method iterates through any controls contained in the Control passed to the method and calls SetToolTip() on them, also. Obviously, you could go to any depth you like.

    Paul T.

    Saturday, January 7, 2012 4:20 AM
  • Hey, why don't just put a div around your usercontrol and give that div a title ! It will work as tooltip of your control.

    Sameer

    • Proposed as answer by Chris Raisin Sunday, August 23, 2020 4:00 AM
    • Unproposed as answer by Chris Raisin Sunday, August 23, 2020 4:00 AM
    Wednesday, August 28, 2019 11:51 AM
  • This is how I achieved it (I use Visual Basic, but you can adapt to C# if required).

    Step 1:  On your User Defined Control (hereinafter referred to as UDC) add a Tooltip from the Toolbox and name
                 this component "TooltipOnControl" (to make it easier to find later and separate it from other Tooltips).
                 You do this by selecting the Tooltip component showing under your UDC design on the "Design" workspace
                 within VS and then changing some Properties in the Properties explorer. Change the name property from
                 "Tooltip1" to "TooltipOnControl".

    Step 2:  While still in the Properties of this component change "Modifiers" from "Friend" (default) to "Public".
                 This ensures that this component on your UDC will be visible on the Control's Parent Form in the VS 

    Step 3:   Within the "Load" procedure of your UDC (if you don't have one, then create one) add the following line of code.
                  This will ensure that no matter what part of your control is sensing the mouse movement, the tooltip will
                  activate (if you do not do this then the tooltip will only display if the user selects the very bottom control
                  on your UDC (usually a panel or GroupBox) and the free space around other controls may be too narrow
                  to activate the tooltip. (Once the user moves on to other controls on your UDC the tooltip would disappear)

             Initialize.Tooltips(Me, Me.ToolTipOnControl)

    Step 3:  Create a separate Module  Named "Initialize" and within that module place the following code:

    Module Initialize
        Public Sub Tooltips(oControl As Control, oTooltip As ToolTip)
            For Each oCntl As Control In oControl.Controls
                oTooltip.SetToolTip(oCntl, oTooltip.GetToolTip(oControl))
                For Each oCntl2 As Control In oCntl.Controls
                    oTooltip.SetToolTip(oCntl2, oTooltip.GetToolTip(oControl))
                    For Each oCntl3 As Control In oCntl2.Controls
                        oTooltip.SetToolTip(oCntl3, oTooltip.GetToolTip(oControl))
                        For Each oCntl4 As Control In oCntl3.Controls
                            oTooltip.SetToolTip(oCntl4, oTooltip.GetToolTip(oControl))
                        Next
                    Next
                Next
            Next
        End Sub
    End Module

    Step 4: Build your UDC by selecting the design workarea of your UDC (where your UDC is appears visually) and cliking on 
                "Build" in the menu then selecting "Build"....(name of UDC)

    Step 5: In the project where you have added your UDC on to a form, make sure you have also added a Tooltip to the
                control which hosts your UDC (usually the Main Form). Leave this component's name as "Tootltip1"

    Step 6:  Select your added UDC and look at it's properties in this new environemtn. You shoudl see TWO tooltips.
                "Tooltip on Tooltip1"  and "TooltipOnControl" (This is why we namedit "TooltipOnControl" so that it would be 
                easily found in the properties just under the project's tooltip.

    Step 7:  Change the wording under "Tooltip on Tooltip1" to the words you wish to be displayed when the user passes
                 the mouse over your UDC.

    Step 8:   Click on the pull-down selection showing next to "TooltipOnControl" and select "Tooltip1".
                  This ensure that when the user moves the mouse over your UDC, then the message in Tooltip1 will be passed
                  to your UDC and will trigger the stored message when any control on your UDC senses the mouse movement.

    I am assuming that there are no more than 4 levels of subcontrols on your UDC. If you find any part of your UDC does not 
    sense the mouse movement, just add another lines in the loops in the "Initialize" procedure (i.e. incorporate oCntl5)

    This seems to work very successfully.

    Any changes you want to make to the properties of the Tooltip should be made through the "Tooltip1" component of the
    main project NOT through the ToolTipOnControl component on your UDC.

         


    Sunday, August 23, 2020 4:04 AM