Important alert: (current site time 7/15/2013 8:56:30 AM EDT)
|
|
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...
- 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.
- 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.
- You may link to this article from another website, but ONLY if it is not wrapped in a frame.
- 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>
</summary>
Public Class BalloonHelp
#Region " Enumerations: Global "
<summary>
''' </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>
''' </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>
</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>
</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>
</summary>
<param name="ctlSource"></param>
<param name="strToolTipText"></param>
<param name="strToolTipTitle"></param>
<param name="enmToolTipIcon"></param>
<remarks></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>
</summary>
<param name="ctlSource"></param>
<param name="strToolTipText"></param>
<param name="strToolTipTitle"></param>
<param name="clrBackColor"></param>
<param name="clrForeColor"></param>
<param name="enmToolTipIcon"></param>
<param name="intAutoPopDelay"></param>
<param name="intInitialDelay"></param>
<param name="intReshowDelay"></param>
<param name="blnIsBalloon"></param>
<param name="blnUseAnimation"></param>
<param name="blnUseFading"></param>
<param name="blnActive"></param>
<param name="blnShowAlways"></param>
<remarks></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
.AutoPopDelay = intAutoPopDelay
.InitialDelay = intInitialDelay
.ReshowDelay = intReshowDelay
.BackColor = clrBackColor
.ForeColor = clrForeColor
.IsBalloon = blnIsBalloon
.ToolTipIcon = enmToolTipIcon
.UseAnimation = blnUseAnimation
.UseFading = blnUseFading
.Active = blnActive
.ShowAlways = blnShowAlways
.ToolTipTitle = strToolTipTitle
.SetToolTip(ctlSource, strToolTipText)
End With
End Sub
#End Region
#Region " Properties: Balloon Help / ToolTip for all Controls "
<summary>
</summary>
<returns></returns>
Public ReadOnly Property TipBackColour() As Color
Get
Return Info
End Get
End Property
<summary>
</summary>
<returns></returns>
Public ReadOnly Property TipTextColour() As Color
Get
Return InfoText
End Get
End Property
#End Region
End Class
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).
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).
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
|
Your Vote
|
| |
Other User Comments
|
2/17/2009 7:53:30 AM: bus
Excelent
Works only with textboxes? (If this comment was disrespectful, please report it.)
| 2/26/2009 6:18:48 PM: Freshbru
@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 AM: bus
A button
Thanks (If this comment was disrespectful, please report it.)
| 3/13/2009 5:31:52 PM: Freshbru
@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 PM: bus
Excelent work
Thanks (If this comment was disrespectful, please report it.)
| 3/17/2009 6:09:11 AM: Raul 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 AM: Freshbru
@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 PM: Freshbru
@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 PM: Kevin 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 AM: Sam 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 AM: hh
how can i use this code.. sory im a beginner.. (If this comment was disrespectful, please report it.)
| 9/6/2009 1:06:22 AM: John 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 AM: John 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 AM: yosyos
Pefect (If this comment was disrespectful, please report it.)
| 1/11/2011 9:30:32 PM: elmaulo
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.
|
|