article

Balloon Tips using EDITBALLOONTIP structure & ToolTip class (not sys tray)

Email
Submitted on: 1/5/2015 10:29:00 PM
By: John Black (from psc cd)  
Level: Intermediate
User Rating: By 8 Users
Compatibility: VB.NET
Views: 6393
 
     Balloon Help Version 2 2 Methods of displaying Balloon Tip Help: 1. Uses EDITBALLOONTIP structure, EM_SHOWBALLOONTIP message and SendMessage to add balloon tip help to your textboxes 2. Uses ToolTip class to add balloon tip help to your other form controls (except ListViews, ListBoxs, TreeViews and RichTextBoxes)


 
				

Imports System.Drawing.SystemColors
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

''' <summary>
''' Class providing method for showing Balloon Tips
''' </summary>
Public Class BalloonHelp

#Region " Enumerations: Global "

  ''' <summary>
  ''' Balloon Icon Types
  ''' </summary>
  Public Enum BalloonIcon
    ShowNone = ToolTipIcon.None
    ShowInformation = ToolTipIcon.Info
    ShowWarning = ToolTipIcon.Warning
    ShowError = ToolTipIcon.Error
  End Enum

#End Region

#Region " Private Declarations: Balloon Help for Textboxes "

  Private Const EM_SHOWBALLOONTIP As UInteger = &H1503

  ''' <summary>
  ''' Type EDITBALLOONTIP converted to a .NET Structure
  ''' </summary>
  <StructLayout(LayoutKind.Sequential)> _
  Private Structure EDITBALLOONTIP
    Public cbStruct As Integer
    <MarshalAs(UnmanagedType.LPWStr)> Public pszTitle As String
    <MarshalAs(UnmanagedType.LPWStr)> Public pszText As String
    Public ttiIcon As Integer
  End Structure

  ''' <summary>
  ''' Unicode version of SendMessage API needed for pszText + pszText in EDITBALLOONTIP as these are unicode parameters
  ''' </summary>
  Private Declare Unicode Function SendMessage Lib "User32" Alias "SendMessageW" (ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

#End Region

#Region " Private Declarations: Balloon Help / ToolTip for all Controls "

  ''' <summary>
  ''' Delay times for ToolTips
  ''' </summary>
  Private Const DELAY_AUTOPOPUP As Integer = 5000
  Private Const DELAY_INITIAL As Integer = 500
  Private Const DELAY_RESHOW As Integer = 500

#End Region

#Region " Subroutines: Balloon Help for Textboxes "

  ''' <summary>
  ''' Balloon Help for Textboxes
  ''' </summary>
  ''' <param name="ctlSource">The textbox you want the ToolTip to be displayed under</param>
  ''' <param name="strToolTipText">The ToolTip text</param>
  ''' <param name="strToolTipTitle">The ToolTip caption</param>
  ''' <param name="enmToolTipIcon">The ToolTip icon - None, Info, Warning or Error</param>
  ''' <remarks>Use this method for textboxes as additional system balloon notifications are displayed and restrictions implemented. If the textbox is a password field, the user will be prompted if the capslock is on and will also not be able to cut text</remarks>
  Public Shared Sub Show(ByVal ctlSource As Control, ByVal strToolTipText As String, ByVal strToolTipTitle As String, Optional ByVal enmToolTipIcon As BalloonIcon = BalloonIcon.ShowInformation)

    Dim EBT As EDITBALLOONTIP = New EDITBALLOONTIP

    With EBT
      .cbStruct = Marshal.SizeOf(EBT)
      .ttiIcon = enmToolTipIcon
      .pszText = strToolTipText
      .pszTitle = strToolTipTitle
    End With

    Dim ptrEBT As IntPtr = Marshal.AllocHGlobal(EBT.cbStruct)

    Marshal.StructureToPtr(EBT, ptrEBT, False)

    Call SendMessage(ctlSource.Handle, EM_SHOWBALLOONTIP, IntPtr.Zero, ptrEBT)

  End Sub

#End Region

#Region " Subroutines: Balloon Help / ToolTip for all Controls "

  ''' <summary>
  ''' Balloon Help / ToolTip for all Controls
  ''' </summary>
  ''' <param name="ctlSource">The control you want the ToolTip to be displayed under</param>
  ''' <param name="strToolTipText">The ToolTip text</param>
  ''' <param name="strToolTipTitle">The ToolTip caption</param>
  ''' <param name="clrBackColor">The backcolor of the ToolTip (any System.Drawing.Color)</param>
  ''' <param name="clrForeColor">The text colour of the ToolTip (any System.Drawing.Color)</param>
  ''' <param name="enmToolTipIcon">The ToolTip icon - None, Info, Warning or Error</param>
  ''' <param name="intAutoPopDelay">The period of time the ToolTip remains visible on control mousehover event</param>
  ''' <param name="intInitialDelay">The period of time before the ToolTip appears with mouseover event</param>
  ''' <param name="intReshowDelay">The period of time before subsequent ToolTip windows appear</param>
  ''' <param name="blnIsBalloon">Show as a balloon tip (True) or a standard tooltip (False)</param>
  ''' <param name="blnUseAnimation">Use animation - Only XP, Windows Server 2003, IE 5+</param>
  ''' <param name="blnUseFading">Use fading - Only XP, Windows Server 2003, IE 5+</param>
  ''' <param name="blnActive">Enable the tooltip</param>
  ''' <param name="blnShowAlways">Force the ToolTip text to be displayed regardless if form has focus or not</param>
  ''' <remarks>This method can be used with all form controls except ListViews, ListBoxs, TreeViews and RichTextBoxes</remarks>
  Public Sub Show(ByVal ctlSource As Control, ByVal strToolTipText As String, ByVal strToolTipTitle As String, ByVal clrBackColor As Color, ByVal clrForeColor As Color, Optional ByVal enmToolTipIcon As BalloonIcon = BalloonIcon.ShowNone, Optional ByVal intAutoPopDelay As Integer = DELAY_AUTOPOPUP, Optional ByVal intInitialDelay As Integer = DELAY_INITIAL, Optional ByVal intReshowDelay As Integer = DELAY_RESHOW, Optional ByVal blnIsBalloon As Boolean = True, Optional ByVal blnUseAnimation As Boolean = True, Optional ByVal blnUseFading As Boolean = True, Optional ByVal blnActive As Boolean = True, Optional ByVal blnShowAlways As Boolean = False)

    Dim MyToolTip As New ToolTip()

    With MyToolTip

      ' Set up the delays for the ToolTip.

      .AutoPopDelay = intAutoPopDelay
      .InitialDelay = intInitialDelay
      .ReshowDelay = intReshowDelay

      ' Set up the appearance of the ToolTip

      .BackColor = clrBackColor
      .ForeColor = clrForeColor
      .IsBalloon = blnIsBalloon
      .ToolTipIcon = enmToolTipIcon
      .UseAnimation = blnUseAnimation
      .UseFading = blnUseFading

      ' Set up the ToolTip display options

      .Active = blnActive
      .ShowAlways = blnShowAlways

      ' Set ToolTip Caption and Text

      .ToolTipTitle = strToolTipTitle
      .SetToolTip(ctlSource, strToolTipText)

    End With

  End Sub

#End Region

#Region " Properties: Balloon Help / ToolTip for all Controls "

  ''' <summary>
  ''' System defined ToolTip background colour
  ''' </summary>
  ''' <returns>System.Drawing.SystemColors.Info</returns>
  Public ReadOnly Property TipBackColour() As Color
    Get
      Return Info
    End Get
  End Property

  ''' <summary>
  ''' System defined ToolTip text colour
  ''' </summary>
  ''' <returns>System.Drawing.SystemColors.InfoText</returns>
  Public ReadOnly Property TipTextColour() As Color
    Get
      Return InfoText
    End Get
  End Property

#End Region

End Class

Usage:

For a Textbox Information Balloon tip...


  Call BalloonHelp.Show(myTextbox, Message, Caption, BalloonHelp.BalloonIcon.ShowInformation)

  Where myTextbox is the control you want the ToolTip to be displayed under,
  Where Message is the ToolTip Message text,
  Where Caption is the ToolTip Title text,
  Where BalloonHelp.BalloonIcon is the ToolTip icon (None, Information, Warning or Error).

For a Button Information Balloon tip...

  Dim ttpButton As New BalloonHelp

  With ttpButton
    .Show(myButton, Message, Caption, .TipBackColour, .TipTextColour, BalloonHelp.BalloonIcon.ShowInformation)
  End With

  Where myButton is the Button you want the ToolTip to be displayed above,
  Where Message is the ToolTip Message text,
  Where Caption is the ToolTip Title text,
  Where .TipBackColour is the system default ToolTip background colour (BalloonHelp.TipBackColour property),
  Where .TipTextColour is the system default ToolTip text colour (BalloonHelp.TipTextColour property),
  Where BalloonHelp.BalloonIcon is the ToolTip icon (None, Information, Warning or Error).

For a customised PictureBox Information Balloon tip with a black background and white text...

  Imports System.Drawing.Color

  ...

  Dim ttpPictureBox As New BalloonHelp

  ttpPictureBox.Show(myPictureBox, Message, Caption, Black, White, BalloonHelp.BalloonIcon.ShowInformation)

  Where myPictureBox is the PictureBox you want the ToolTip to be displayed above,
  Where Message is the ToolTip Message text,
  Where Caption is the ToolTip Title text,
  Where Black is the ToolTip background colour = System.Drawing.Color.Black,
  Where White is the ToolTip background colour = System.Drawing.Color.White,
  Where BalloonHelp.BalloonIcon is the ToolTip icon (None, Information, Warning or Error).


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.