【问题标题】:How can I show a ToolTip for a TextBox only under certain conditions如何仅在某些条件下显示 TextBox 的工具提示
【发布时间】:2014-03-07 19:22:13
【问题描述】:

在 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

【问题讨论】:

  • 为什么不使用内置的 ToolTip 组件呢?那些 VB6 声明在 VB.Net 中不起作用。

标签: vb.net api tooltip popup-balloons


【解决方案1】:

你可以这样设置工具提示

Dim toolTip1 As New ToolTip()

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

并隐藏工具提示

toolTip1.SetToolTip(Me.textbox1, "")

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

【讨论】:

    【解决方案2】:

    我认为只能使用 API 方式以我想要的方式完成。 我在这里找到了我的 VB6 示例的完美迁移:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-28
      相关资源
      最近更新 更多