Click here to Skip to main content
15,915,501 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am able to do this easily enough through the project itself by changing the ownerdraw to true but I have a number of modules to the project (separate applications) so would like to have one DLL class that I can import and call as necessary.

So if I change the ownerdraw to true in the tooltip property and place the following lines of code in the ondraw event, I can do whatever I want as I have essentially taken over the tooltip popup.

dim NewFrm as new Form2
     NewFrm.Show



but what I'd like to do, instead of placing a standard tooltip control in all of my forms, across several projects that are linked together, is create a DLL or toolbox control that works just like a tooltip control but does what I want it to do, when the event kicks off on the ondraw.


Imports System.Windows.Forms

Public Class NewToolTip

    Inherits System.Windows.Forms.ToolTip

        Private WithEvents toolTip As System.Windows.Forms.ToolTip

        Private Sub toolTip_Draw(ByVal sender As System.Object,
            ByVal e As DrawToolTipEventArgs) Handles toolTip.Draw

            Dim NewForm As New MyToolTipForm
            NewForm.Show()

        End Sub


End Class


I have tried searching for example across codeproject and stackoverflow as well as a general google search but Im not getting what I am looking for - any tips or directions would be great, thanks

What I have tried:

searching on google, codeproject, stackoverflow and anywhere where it had custom tooltips or how to override tooltips
Posted
Updated 25-Jan-19 10:18am

1 solution

Hi,

You could use the code underneath and attach it to each form on which you want the existing ToolTips to be intercepted. Just be sure those ToolTips are part of the components container of this form.

Imports System.Windows.Forms

Public Class TooltipStopper
    Inherits NativeWindow
    Implements IDisposable

    Dim TTs As Generic.List(Of ToolTip)

    Public Sub New(Frm As Form)
        'By use of reflection the ToolTipStopper will search for the "components" container on the form.
        'This container should contain all the ToolTips created on the form.
        Dim t As Type = Frm.GetType
        Dim obj As Object = t.InvokeMember("components", Reflection.BindingFlags.GetField Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic, Nothing, Frm, Nothing)
        If Not obj Is Nothing Then
            Dim components As System.ComponentModel.Container = CType(obj, System.ComponentModel.Container)
            For i As Integer = 0 To components.Components.Count - 1
                If TypeOf components.Components(i) Is ToolTip Then
                    'If a ToolTip has been found, add it to a collection.
                    Dim TT As ToolTip = CType(components.Components(i), ToolTip)
                    If TTs Is Nothing Then
                        TTs = New Generic.List(Of ToolTip)
                    End If
                    TTs.Add(TT)
                    'Defer the Popup event of the ToolTip to a procedure
                    AddHandler TT.Popup, AddressOf TT_Popup
                End If
            Next
        End If
    End Sub

    Private Sub TT_Popup(sender As Object, e As PopupEventArgs)
        'Show a replacement for the ToolTip here and cancel the original ToolTip
        e.Cancel = True
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        'Don't forget to remove the handlers for all the ToolTips found on the form
        If Not TTs Is Nothing Then
            For i As Integer = 0 To TTs.Count - 1
                RemoveHandler TTs(i).Popup, AddressOf TT_Popup
            Next
        End If
    End Sub
End Class


In the form you would then write something like this:

Public Class Form1
    Dim ToolTipStop As TooltipStopper.TooltipStopper

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        'bind the ToolTipStopper to the form
        ToolTipStop = New TooltipStopper.TooltipStopper(Me)

        'set the tooltip(s) to the controls you want
        Me.ToolTip1.SetToolTip(Me.Button1, "test")
    End Sub

    Private Sub Form1_Disposed(sender As Object, e As EventArgs) Handles Me.Disposed
        'be sure to dispose the ToolTipStopper
        Me.ToolTipStop.Dispose()
    End Sub
End Class


Hope this helps!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900