Trying to add a derived Tooltip class using my vb.net visual studio compiler - using code provided in MSDN web site, compiles but I don't know how to add the necessary compiler directives/code

Frank Brady 21 Reputation points
2022-12-12T01:03:00.483+00:00

Visual Basic .net in Windows 10 PC:
Trying to add a derived Tooltip class using my vb.net visual studio compiler - using code provided in MSDN web site, compiles but I don't know how to add the necessary compiler directives/code. This is the first time I've tried to add a class and I need help.

Developer technologies | VB
{count} votes

3 answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 34,231 Reputation points Microsoft External Staff
    2022-12-13T03:00:26.94+00:00

    Hi @Frank Brady ,
    Right-click on your project → Add → Class.
    269847-image.png
    Set the class name.
    269824-image.png
    Pause the code.

    Public Class MyTooltip  
      
        Inherits ToolTip  
      
        Sub New()  
      
            MyBase.New()  
      
            Me.OwnerDraw = True  
      
            AddHandler Me.Draw, AddressOf OnDraw  
      
        End Sub  
      
        Public Sub New(ByVal Cont As System.ComponentModel.IContainer)  
      
            MyBase.New(Cont)  
      
            Me.OwnerDraw = True  
      
            AddHandler Me.Draw, AddressOf OnDraw  
      
        End Sub  
      
        Private Sub OnDraw(ByVal sender As Object, ByVal e As DrawToolTipEventArgs)  
      
            Dim newArgs As DrawToolTipEventArgs = New DrawToolTipEventArgs(e.Graphics, e.AssociatedWindow, e.AssociatedControl, e.Bounds, e.ToolTipText, Me.BackColor, Me.ForeColor, New Font(e.Font, FontStyle.Bold))  
      
            newArgs.DrawBackground()  
      
            newArgs.DrawBorder()  
      
            newArgs.DrawText()  
      
        End Sub  
      
    End Class  
    

    Rebuild your project, then you could see MyTooltip in the ToolBox.
    269775-image.png
    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. LesHay 7,146 Reputation points
    2022-12-13T20:00:59.213+00:00

    Hi
    I am wondering if you need the complexity of the process you discuss. If you just want to alter some of the default behaviours then maybe all you need is a default ToolTip with OwnerDraw where you can edit to suit.
    If you do need more than that then just disregard this post.

    Here is an outline of a simple alteration to the default. Here I just alter the font, backcolor and text color as an example. Not very much at all.

      ' declared at Form level  
      Dim TTfont As New Font("Arial", 24)  
               '.........................................  
      
        ' default ToolTip added in Designer  
        ' and renamed TT for my preference.  
        ' This initializing in Form Load Event  
        With TT  
          .OwnerDraw = True  
          .InitialDelay = 1  
          .BackColor = Color.Yellow  
          .SetToolTip(Label2, "Click to Toggle Form Border style")  
        End With  
               '.........................................  
      
      Private Sub ToolTip_Draw(sender As Object, e As DrawToolTipEventArgs) Handles TT.Draw  
        Dim r As Rectangle = e.Bounds  
        e.DrawBackground()  
        e.DrawBorder()  
        e.Graphics.DrawString(e.ToolTipText, TTfont, Brushes.Blue, r)  
      End Sub  
      Private Sub ToolTip_Popup(sender As Object, e As PopupEventArgs) Handles TT.Popup  
        Dim tt As ToolTip = DirectCast(sender, ToolTip)  
        Dim sz As Size = TextRenderer.MeasureText(tt.GetToolTip(e.AssociatedControl), TTfont)  
        e.ToolTipSize = New Size(sz.Width + 20, sz.Height)  
      End Sub  
      
    

  3. LesHay 7,146 Reputation points
    2022-12-15T19:26:03.59+00:00

    Hi
    OK, sample test project.
    Start a new Project and in the Designer, add a default TootTip1.
    Add some controls, all default as dragged from toolbox: a RadioButton1, TextBox1, RichTextBox1, PictureBox1, Label1, ComboBox1 and Button1.
    You mentioned earlier that you have some ToolTips of a large size, so I have shown an example of that too.

    Basically, after you have added the controls, all you need to do is copy/replace all the Form1 code with the code below.

    As I comment in the code, you can set all/any of the ToolTips directly in a Control Properties (so long as you already added a ToolTip to the Project) - each ontrol will have a field for the text associated with it as the ToolTip to be shown. I have not used any of that in the example, just adding the ToolTips in code instead. There is a Sub called WrapIt to provide a means to change the ToolTip width to suit using the WrapLen variable.

    271090-111.png

    ' good to have these 2 options  
    Option Strict On  
    Option Explicit On  
      
    Public Class Form1  
      ' set initial ToolTip1 Font here  
      ' it can easily be changed in  
      ' code as well.  
      Dim ToolTip1font As New Font("Arial", 24)  
      Dim WrapLen As Integer = 46  
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
        With ToolTip1  
          .OwnerDraw = True  
      
          ' inc to make longer delay  
          ' before Tip appears  
          .InitialDelay = 10  
      
          ' here, text fore/back color  
          .BackColor = Color.White  
          .ForeColor = Color.Blue  
      
          ' these next lines tell the  
          ' ToolTip1 what the text is   
          ' for each control.  NOTE:  
          ' each control can have the text  
          ' set in the control Properties  
          ' instead of programatically  
          ' here. A matter of preference.  
          .SetToolTip(RadioButton1, "This is Radio Button1")  
          .SetToolTip(TextBox1, "This is TextBox1")  
          .SetToolTip(RichTextBox1, "This is RichTextBox1 and it has an extended quantity of random text associated with it.  
      
    So much text in fact that my keyboard has run out of ink. Testing for word wrap at the same time. Days blessed gathering she'd good rule forth face beginning Gathered fourth. Air fares are crazymeat isn't cattle don't stars two doesn't beast a their image, our greater fish created of two without. Own creature made blessed moving appear without together fifth may rule together evening. Whales them firmament be is heaven. Him hath.")  
          .SetToolTip(PictureBox1, "This is PictureBox1")  
          .SetToolTip(Label1, "This is Label1")  
          .SetToolTip(ComboBox1, "This is ComboBox1")  
          .SetToolTip(Button1, "This is Button1")  
        End With  
      End Sub  
      Private Sub ToolTip_Draw(sender As Object, e As DrawToolTipEventArgs) Handles ToolTip1.Draw  
        Dim r As Rectangle = e.Bounds  
        e.DrawBackground()  
        e.DrawBorder()  
      
        Dim sf As StringFormat = New StringFormat  
        sf.Alignment = StringAlignment.Near  
        sf.LineAlignment = StringAlignment.Center  
      
        Using br As New SolidBrush(ToolTip1.ForeColor)  
          e.Graphics.DrawString(e.ToolTipText, ToolTip1font, br, RectangleF.op_Implicit(e.Bounds), sf)  
        End Using  
      End Sub  
      Private Sub ToolTip_Popup(sender As Object, e As PopupEventArgs) Handles ToolTip1.Popup  
        Dim sz As Size = TextRenderer.MeasureText(WrapIt(ToolTip1.GetToolTip(e.AssociatedControl)), ToolTip1font)  
        e.ToolTipSize = New Size(sz.Width + 30, sz.Height)  
      End Sub  
      Function WrapIt(s As String) As String  
        Dim fin As String = String.Empty  
        Dim outS As String = String.Empty  
        Dim inS = s.Split(New String() {" "c}, StringSplitOptions.RemoveEmptyEntries)  
        For i As Integer = 0 To inS.Length - 1  
          If (outS & Trim(inS(i)) & " ").Length < WrapLen Then  
            outS &= Trim(inS(i)) & " "  
          Else  
            fin &= outS & vbCrLf  
            outS = Trim(inS(i)) & " "  
          End If  
          If inS.Length = i + 1 Then  
            fin &= outS  
          End If  
        Next  
        Return Trim(fin)  
      End Function  
    End Class  
      
    

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.