Results 1 to 12 of 12

Thread: [RESOLVED] Dynamically adding tool-tips to controls

  1. #1

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Resolved [RESOLVED] Dynamically adding tool-tips to controls

    Objective: I want to specify a control in a form to display a tooltip dynamically to that object when the mouse is hovered. I tried to fool around a little bit and I have this... I am probably not close to what I need to achieve but I gave it a shot.

    I know one fundamental problem, Me.MouseHover only works on the form itself and not the controls of that form.

    Code:
        Public Sub determineControl() Handles Me.MouseHover
            Dim c As Control = Me
    For Each ctrl As Control In c.Controls
                If TypeOf ctrl Is GroupBox Then
                    Dim tip As New ToolTip
                    tip.SetToolTip(ctrl, ctrl.Tag)
                End If
            Next
    
        End Sub
    The issue is that when I hover over a control in the form, it doesn't generate the tooltip. How do I get this code to do what it should do? It seems to look write but when it runs, and you hover over a control, it doesn't actually create a tooltip.

    Also I will create the tooltip on load and just change its properties based on the control its on.

    EDIT: I just had another read of what you've said. Do I change the handler to MouseMove?

    HUGE EDIT: I am using a tab pages. I want it to loop through the groupboxes in the tab page!
    I want it to loop through all the controls in the selected tab page.
    Last edited by Reapism; Mar 29th, 2016 at 08:38 PM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Dynamically adding tool-tips to controls

    Okay, so what's the issue? That's not the correct signature for a method that handles a MouseHover event, since it takes the wrong arguments, but maybe you left the signature out of the post.

    I took a look at some code I had that dealt with it, and notice that I was setting the tooltip in MouseMove, but only if the current text associated with the control has changed. That was because the tooltips I was displaying depended on what part of a control the mouse was over.

    Ah, I noticed one key mistake: You don't create a ToolTip like that. Add one to the form at design time by dragging it from the Toolbox. It doesn't matter whether you set text for it at design time, and if you have no text associated with a control, it won't show at all. So, you want the tooltip already there. What you are doing dynamically, is changing whether or not it is shown and what is shown. If you have no text, it won't show, if you set text, then it will.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Re: Dynamically adding tool-tips to controls

    The issue is that when I hover over a control in the form, it doesn't generate the tooltip. How do I get this code to do what it should do? It seems to look write but when it runs, and you hover over a control, it doesn't actually create a tooltip.

    Also I will create the tooltip on load and just change its properties based on the control its on.

    EDIT: I just had another read of what you've said. Do I change the handler to MouseMove?

    HUGE EDIT: I am using a tab pages. I want it to loop through the groupboxes in the tab page!
    I want it to loop through all the controls in the selected tab page.
    Last edited by Reapism; Mar 29th, 2016 at 08:35 PM.

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: Dynamically adding tool-tips to controls

    sh, said
    Okay, so what's the issue? That's not the correct signature for a method that handles a MouseHover event, since it takes the wrong arguments, but maybe you left the signature out of the post.
    This is what the form MouseHoover Event looks like.
    Code:
        Private Sub Form1_MouseHover(sender As Object, e As System.EventArgs) Handles Me.MouseHover
    
        End Sub

  5. #5
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Dynamically adding tool-tips to controls

    Quote Originally Posted by Shaggy Hiker View Post
    Okay, so what's the issue? That's not the correct signature for a method that handles a MouseHover event, since it takes the wrong arguments, but maybe you left the signature out of the post.
    From .Net Framework 4 (or so) onwards, you are allowed to leave out the (Sender As Object, e As EventArgs) bit if you don't need the arguments. I understand that's something to do with Relaxed Delegates.

    @Reapism: if you want to do it all in code, you can use just one tooltip and set the appropriate captions all in one place, for example the form's Load sub:
    Code:
      Private Sub Form1_Load() Handles MyBase.Load
          Dim tt As New ToolTip
          tt.SetToolTip(Me, "This is your Form speaking.")
          tt.SetToolTip(GroupBox1, "Hi, I'm GroupBox 1!")
          tt.SetToolTip(GroupBox2, "Help, a mouse (pointer)!")
    
          For Each tPage As TabPage In TabControl1.TabPages
             For Each ctrl As Control In tPage.Controls
                tt.SetToolTip(ctrl, "Hi, I'm " & ctrl.Name & " on " & tPage.Name)
                'etc.
             Next
          Next
    
          '...and so on for all your controls
       End Sub
    This way of doing it is needed if you want to change the caption texts dynamically at runtime. Otherwise, if you have a lot of controls with different texts, doing it in the designer will probably be easier.

    BB

  6. #6
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    482

    Re: Dynamically adding tool-tips to controls

    If the tooltips are not going to change for a given control then it is MUCH better to do this in the Designer and forget about it.

    If the tooltips are not going to change BUT you HAVE to do it in code, then the solution by boops boops is the best route.

    If the tooltips will change for any given control then you need to decide if it will be by control name(TextBox1, TextBox2, etc.) or by control type (TextBox, ComboBox, Button, etc.)

    By name sample, you will need this block for each control:
    NOTE: WHATEVERCONTROLNAME will be changed to the specific control name, and the first line Dim change to the proper type for the control being used.
    Code:
        Private Sub WHATEVERCONTROLNAME_MouseHover(sender As Object, e As System.EventArgs) Handles WHATEVERCONTROLNAME.MouseHover
                   Dim sndr as TextBox
                   Dim tt As New ToolTip
                   tt.SetToolTip(sndr, "This is your tip.")
        End Sub
    By type sample:
    NOTE1: You will need to add all of your controls that you want to add tips to, to the Handles clause of the Sub.
    NOTE2: It might be possible to attach the ToolTip to sender directly without the Select Case but I am not sure, I haven't tried it either way.
    Code:
        Private Sub Multi_MouseHover(sender As Object, e As System.EventArgs) Handles Control1.MouseHover, Control2.MouseHover, Control3.MouseHover
                   Dim type As String = sender.GetType.ToString
                   Select Case type
                   Case type = "TextBox"
                       Dim sndr as TextBox
                   Case type = "ComboBox"
                       Dim sndr as ComboBox
                   Case type = "Button"
                       Dim sndr as Button
                   End Select
                   Dim tt As New ToolTip
                   tt.SetToolTip(sndr, "This is your tip.")
        End Sub

  7. #7

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Re: Dynamically adding tool-tips to controls

    Quote Originally Posted by boops boops View Post
    From .Net Framework 4 (or so) onwards, you are allowed to leave out the (Sender As Object, e As EventArgs) bit if you don't need the arguments. I understand that's something to do with Relaxed Delegates.

    @Reapism: if you want to do it all in code, you can use just one tooltip and set the appropriate captions all in one place, for example the form's Load sub:
    Code:
      Private Sub Form1_Load() Handles MyBase.Load
          Dim tt As New ToolTip
          tt.SetToolTip(Me, "This is your Form speaking.")
          tt.SetToolTip(GroupBox1, "Hi, I'm GroupBox 1!")
          tt.SetToolTip(GroupBox2, "Help, a mouse (pointer)!")
    
          For Each tPage As TabPage In TabControl1.TabPages
             For Each ctrl As Control In tPage.Controls
                tt.SetToolTip(ctrl, "Hi, I'm " & ctrl.Name & " on " & tPage.Name)
                'etc.
             Next
          Next
    
          '...and so on for all your controls
       End Sub
    This way of doing it is needed if you want to change the caption texts dynamically at runtime. Otherwise, if you have a lot of controls with different texts, doing it in the designer will probably be easier.

    BB
    Based on your input, I was able to get exactly what I want.

    In the load method, I just call the method.

    This is the method.

    Code:
    Public Sub determineControl() Handles Me.MouseHover        
    For Each tPage As TabPage In tabSettings.TabPages
                For Each ctrl As Control In tPage.Controls
                    Dim tip As New ToolTip
                    tip.ToolTipTitle = ctrl.Text
                    tip.SetToolTip(ctrl, "Help: " & ctrl.Tag)
                Next
            Next
    End Sub
    How does that look?

  8. #8

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Re: Dynamically adding tool-tips to controls

    This gave me good insight as to how I wanted to approach this. Essentially in each group box, I specify its tag property with the help text I want to display. Works like a charm!

  9. #9

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Re: Dynamically adding tool-tips to controls

    Last post on the code. I have gotten something that is quite nice.

    A description of the method is provided in the comment block.
    Code:
        ''' <summary>
        ''' determineControl()
        ''' Description: In each tabpage in tabSettings, it will look for each control that
        ''' is a GroupBox, and create a help tooltip that is associated with each groupbox
        ''' and list the help from the GroupBox's Tag property.
        ''' </summary>
        Public Sub determineControl() Handles Me.MouseHover
            For Each tPage As TabPage In tabSettings.TabPages
                For Each ctrl As Control In tPage.Controls
                    If TypeOf ctrl Is GroupBox Then
                        Dim tip As New ToolTip
                        tip.ToolTipTitle = ctrl.Text
                        tip.SetToolTip(ctrl, "Displaying help for " & ctrl.Text &
                                       "." & System.Environment.NewLine & ctrl.Tag)
                    End If
                Next
            Next
        End Sub

  10. #10
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Dynamically adding tool-tips to controls

    Quote Originally Posted by Reapism View Post
    Last post on the code. I have gotten something that is quite nice.

    A description of the method is provided in the comment block.
    Code:
        ''' <summary>
        ''' determineControl()
        ''' Description: In each tabpage in tabSettings, it will look for each control that
        ''' is a GroupBox, and create a help tooltip that is associated with each groupbox
        ''' and list the help from the GroupBox's Tag property.
        ''' </summary>
        Public Sub determineControl() Handles Me.MouseHover
            For Each tPage As TabPage In tabSettings.TabPages
                For Each ctrl As Control In tPage.Controls
                    If TypeOf ctrl Is GroupBox Then
                        Dim tip As New ToolTip
                        tip.ToolTipTitle = ctrl.Text
                        tip.SetToolTip(ctrl, "Displaying help for " & ctrl.Text &
                                       "." & System.Environment.NewLine & ctrl.Tag)
                    End If
                Next
            Next
        End Sub
    I have some comments on that.

    Firstly, I wouldn't declare multiple tooltips insided the loop. You can't access those tooltips from elsewhere, e.g. if you want to allow the user to disable tooltips, because the identifier "tip" goes out of scope at the end of the loop. Besides, it's inelegant when one tooltip will do the same job for all the controls on the form.

    More problematic is putting the loop inside a MouseHover event handler. The event could fire thousands of times during the lifetime of your application. That means each control will end up with thousands of tooltips to render, one on top of another. (You can check that this is true by defining 2 tooltips for the same control and giving the first one a longer text than the second one. At runtime you will see the first tooltip poking out behind the second one, meaning both tooltips are drawn.) What is more, the Garbage Collector won't be able to get rid of all those tooltips, even though they are out of scope, because they are attached to controls. It would be quite a burden on your application's performance, and it's so unnecessary when you actually only need one tooltip per form.

    There may be scenarios where you might think of using more than one tooltip object (for example, one per user language), although I expect there would be better ways of doing the job with just one.

    Finally, you could make your code a bit more compact if you want by replacing the loop beginning For Each ctrl As Control ... with this:
    Code:
    For Each ctrl As Control In tPage.Controls.OfType(Of GroupBox)
            tip.ToolTipTitle = ctrl.Text
            '... etc.
    Next
    BB

  11. #11

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Re: Dynamically adding tool-tips to controls

    Quote Originally Posted by boops boops View Post
    I have some comments on that.

    Firstly, I wouldn't declare multiple tooltips insided the loop. You can't access those tooltips from elsewhere, e.g. if you want to allow the user to disable tooltips, because the identifier "tip" goes out of scope at the end of the loop. Besides, it's inelegant when one tooltip will do the same job for all the controls on the form.

    More problematic is putting the loop inside a MouseHover event handler. The event could fire thousands of times during the lifetime of your application. That means each control will end up with thousands of tooltips to render, one on top of another. (You can check that this is true by defining 2 tooltips for the same control and giving the first one a longer text than the second one. At runtime you will see the first tooltip poking out behind the second one, meaning both tooltips are drawn.) What is more, the Garbage Collector won't be able to get rid of all those tooltips, even though they are out of scope, because they are attached to controls. It would be quite a burden on your application's performance, and it's so unnecessary when you actually only need one tooltip per form.

    There may be scenarios where you might think of using more than one tooltip object (for example, one per user language), although I expect there would be better ways of doing the job with just one.

    Finally, you could make your code a bit more compact if you want by replacing the loop beginning For Each ctrl As Control ... with this:
    Code:
    For Each ctrl As Control In tPage.Controls.OfType(Of GroupBox)
            tip.ToolTipTitle = ctrl.Text
            '... etc.
    Next
    BB
    It doesn't seem to run more than once. I have call the method on load.

  12. #12
    New Member
    Join Date
    Nov 2024
    Posts
    1

    Re: Dynamically adding tool-tips to controls

    Quote Originally Posted by Reapism View Post
    It doesn't seem to run more than once. I have call the method on load.
    I know this thread is old and maby outdated, but someone could find this usefull.
    Code:
        Private Sub ShowTooltip(ByVal ControleName As Control, ByVal InputText As String, Optional TextLength As Integer = 40, Optional Titel As String = "Information", Optional DisplayTime As Integer = 5000)
            'Add Private TT1 as New ToolTip in top of Class (Under the classname), to make it work as a form all over tooltip
            Dim ActiveText As String = InputText
            Dim Output As String = String.Empty
            Dim PartOfstring As String = String.Empty
    
            While ActiveText.Length > 1
                If ActiveText.Length < TextLength Then
                    Output &= ActiveText
                    ActiveText = String.Empty
                Else
                    Dim SpaceBar As Integer = 0
                    SpaceBar = InStr(Mid(ActiveText, TextLength, ActiveText.Length), " ")
                    PartOfstring = Mid(ActiveText, 1, TextLength + (SpaceBar - 1)) & Environment.NewLine
                    Output &= PartOfstring
                    ActiveText = Mid(ActiveText, TextLength + SpaceBar, ActiveText.Length)
                End If
            End While
    
            TT1.ToolTipTitle = Titel
            TT1.AutoPopDelay = DisplayTime
            TT1.Show(Output, ControleName)
        End Sub
    You will use it as this example:
    Code:
        Private Sub Lbl_Besked_MouseHover(sender As Object, e As EventArgs) Handles Lbl_Besked.MouseHover
            ShowTooltip(Lbl_Besked, Lbl_Besked.Text,, "Test Info")
        End Sub
    I know you have to write it in all form elements you wanna show tooltip on, but it is working as it should. Use it as an idea or leave it.
    Last edited by Shaggy Hiker; Nov 7th, 2024 at 11:23 AM. Reason: Added CODE tags.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width