Important alert: (current site time 7/15/2013 8:56:30 AM EDT)
 

article

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

Email
Submitted on: 3/13/2009 9:05:02 AM
By: John Black  
Level: Intermediate
User Rating: By 8 Users
Compatibility: VB.NET
Views: 14484
 
     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)


 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				

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).


Other 2 submission(s) by this author

 


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
2/17/2009 7:53:30 AMbus

Excelent

Works only with textboxes?
(If this comment was disrespectful, please report it.)

 
2/26/2009 6:18:48 PMFreshbru

@bus

What type of contol are you trying to get a balloon tip for?
(If this comment was disrespectful, please report it.)

 
3/9/2009 11:48:03 AMbus

A button

Thanks
(If this comment was disrespectful, please report it.)

 
3/13/2009 5:31:52 PMFreshbru

@bus, just resubmitted with added support for most form controls. Please don't forget to rate the code if you like it :)
(If this comment was disrespectful, please report it.)

 
3/16/2009 12:22:32 PMbus

Excelent work

Thanks
(If this comment was disrespectful, please report it.)

 
3/17/2009 6:09:11 AMRaul Costa

Hi,
Great code.
Two things to improve:
1) If the Mouse move, the tooptip should also move.
2) the text should be updateble.
Thanks for this code. Can it be used in comercial software ?

(If this comment was disrespectful, please report it.)

 
3/20/2009 10:43:29 AMFreshbru

@Raul

1) You can move the tooltip using the _mousemove event for the control

i.e.

Private Sub Button1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove

With ttpButton
.Show(Button1, e.X & ":" & e.Y, CAPTION, .TipBackColour, .TipTextColour, ShowInformation, 100)
End With

End Sub

Note: You can try adjusting the 'intAutoPopDelay' value to stop overpainting of the tooltip or you may want to look at the BackgroundWorker class to process form paint events sychronously.

2) I'm not sure what you mean but if you want to change the tooltip text each time the mouse moves just call the .Show method with new values for strToolTipText and/or strToolTipTitle. As per above using e.X & ":" & e.Y you will see the tooltip display the co-ordinates of the mouse pointer...

if you need more help let me know.
(If this comment was disrespectful, please report it.)

 
3/20/2009 1:29:43 PMFreshbru

@Raul

Forgot to mention... Feel free to use this code in comercial software.

Please just credit myself and Planet Source Code in your comments :)
(If this comment was disrespectful, please report it.)

 
7/13/2009 6:44:12 PMKevin S. Gallagher

Good code. The only minor point for me would be not to make up your own enum for the icons. I tweaked it to use the predefined ToolTipIcon enums. Thanks for sharing!!!
(If this comment was disrespectful, please report it.)

 
8/8/2009 8:50:16 AMSam Johnson Clarke

Works as shown. Accept this could confuse new users since it over rides the show event: Me.show() overload fail.... Will need to rename varibles to fix this.
(If this comment was disrespectful, please report it.)

 
9/4/2009 5:50:00 AMhh

how can i use this code.. sory im a beginner..
(If this comment was disrespectful, please report it.)

 
9/6/2009 1:06:22 AMJohn Black

@Sam - I'll think you'll find that the System.Windows.Forms.ToolTip class uses the 'Show' method to display tooltips.

My code mearly keeps in line with the standard implimentation.

Also, if you want to rename anything, I'll think you might want to leave the variable names; try renaming the subroutine 'Show' to something else that won't confuse you.
(If this comment was disrespectful, please report it.)

 
9/6/2009 1:13:40 AMJohn Black

@hh

1. Add a class modules to your VB project.

2. Copy and paste the above code into the class (From Class BalloonHelp .... to End Class).

3. On a windows form, add a textbox.

4. Paste the 1st code example into the Textbox_Textchanged event. Change the 'Message' parameter to the Textbox.text.

5. You should see the text that you are typing into the textbox displayed in a balloon tip.
(If this comment was disrespectful, please report it.)

 
11/27/2009 1:15:51 AMyosyos

Pefect
(If this comment was disrespectful, please report it.)

 
1/11/2011 9:30:32 PMelmaulo

Hi

its a great code, but i have a question. if tooltip has shown, then i click those tooltip, it always show never hide again. how to hide those tooltip again
Note: I use the Balloon Help for Textboxes code.

Thanks
(If this comment was disrespectful, please report it.)

 

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.