如何仅在特定条件下显示TextBox的工具提示

时间:2014-03-07 19:22:13

标签: vb.net api tooltip popup-balloons

在VB6中,我可以轻松创建一个气球消息,该消息将显示在文本框旁边。 一旦文本发生变化,它就会自动消失。 我可以将此气球工具提示用于“输入有效的电子邮件地址!”等消息。

我使用Windows API创建此气球。我附上了以下代码。

这没有框架解决方案吗? 谢谢你的帮助!

Option Explicit

Private Const ECM_FIRST = &H1500                    '// Edit control messages

Private Const EM_SETCUEBANNER = (ECM_FIRST + 1)
Private Const EM_GETCUEBANNER = (ECM_FIRST + 2)     '// Set the cue banner with the lParm = LPCWSTR

Private Type EDITBALLOONTIP
    cbStruct As Long
    pszTitle As Long
    pszText As Long
    ttiIcon As Long ' ; // From TTI_*
End Type

Private Const EM_SHOWBALLOONTIP = (ECM_FIRST + 3)      '// Show a balloon tip associated to the edit control
Private Const EM_HIDEBALLOONTIP = (ECM_FIRST + 4)       '// Hide any balloon tip associated with the edit control
Private Declare Function SendMessageW Lib "user32" ( _
   ByVal hwnd As Long, _
   ByVal wMsg As Long, _
   ByVal wParam As Long, _
   lParam As Any) As Long

Private Declare Function LocalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal wBytes As Long) As Long
Private Declare Function LocalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function LocalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function LocalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Const GMEM_FIXED = &H0
Private Const GMEM_ZEROINIT = &H40
Private Const GPTR = (GMEM_FIXED Or GMEM_ZEROINIT)

Private m_hWnd As Long
Private m_sCueBanner As String
Private m_sTitle As String
Private m_sText As String
Private m_eIcon As BalloonTipIconConstants

Public Property Let TextBox(txtThis As TextBox)
   m_hWnd = txtThis.hwnd
End Property

Public Property Let CueBanner(ByVal value As String)
   m_sCueBanner = value
   setCueBanner
End Property
Public Property Get CueBanner() As String
   CueBanner = m_sCueBanner
End Property
Public Property Let BalloonTipTitle(ByVal value As String)
   m_sTitle = value
End Property
Public Property Get BalloonTipTitle() As String
   BalloonTipTitle = m_sTitle
End Property
Public Property Let BalloonTipText(ByVal value As String)
   m_sText = value
End Property
Public Property Get BalloonTipText() As String
   BalloonTipText = m_sText
End Property
Public Property Let BalloonTipIcon(ByVal value As BalloonTipIconConstants)
   m_eIcon = value
End Property
Public Property Get BalloonTipIcon() As BalloonTipIconConstants
   BalloonTipIcon = m_eIcon
End Property

Public Sub ShowBalloonTip()

Dim lR As Long
   Dim tEBT As EDITBALLOONTIP
   tEBT.cbStruct = LenB(tEBT)
   tEBT.pszText = StrPtr(m_sText)
   tEBT.pszTitle = StrPtr(m_sTitle)
   tEBT.ttiIcon = m_eIcon
   lR = SendMessageW(m_hWnd, EM_SHOWBALLOONTIP, 0, tEBT)
End Sub
Public Sub HideBalloonTip()
    Dim lR As Long
   lR = SendMessageLongW(m_hWnd, EM_HIDEBALLOONTIP, 0, 0)
   Debug.Print lR
End Sub
Private Sub setCueBanner()
Dim lR As Long
   ' Reports success, but doesn'/t actually work...
   ' (is this because the VB text box is ANSI?)
   lR = SendMessageLongW(m_hWnd, EM_SETCUEBANNER, 0, StrPtr(m_sCueBanner))
   Debug.Print lR
End Sub

2 个答案:

答案 0 :(得分:0)

您可以设置这样的工具提示

Dim toolTip1 As New ToolTip()

toolTip1.SetToolTip(Me.textbox1, "Hello World")

并隐藏工具提示

toolTip1.SetToolTip(Me.textbox1, "")

请注意,您还可以将ToolTip - Control添加到工具箱的表单中,而不是以编程方式创建它。它神奇地为属性窗口中的所有控件添加了ToolTip - 属性,您可以在其中输入工具提示消息。但是,控件本身不会获取ToolTip属性,您只能通过代码中的ToolTip.SetToolTip方法设置或删除工具提示。

答案 1 :(得分:0)

我认为它只能以我想要的方式使用API​​方式完成。 我在这里找到了一个完美的VB6样本迁移:

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=7109&lngWId=10