我在互联网上找到了以下代码:
Imports System.Runtime.InteropServices
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SetCueText(TextBox1, "Enter Name here")
End Sub
End Class
Module CueBannerText
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, <MarshalAs(UnmanagedType.LPWStr)> ByVal lParam As String) As Int32
End Function
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As IntPtr, ByVal hWnd2 As IntPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As IntPtr
Private Const EM_SETCUEBANNER As Integer = &H1501
Public Sub SetCueText(ByVal control As Control, ByVal text As String)
If TypeOf control Is ComboBox Then
Dim Edit_hWnd As IntPtr = FindWindowEx(control.Handle, IntPtr.Zero, "Edit", Nothing)
If Not Edit_hWnd = IntPtr.Zero Then
SendMessage(Edit_hWnd, EM_SETCUEBANNER, 0, text)
End If
ElseIf TypeOf control Is TextBox Then
SendMessage(control.Handle, EM_SETCUEBANNER, 0, text)
End If
End Sub
End Module
然而,它给了我以下错误:
'Handles' in modules must specify a 'WithEvents' variable qualified with a single identifier.
有人知道如何实现工作提示横幅,或者如何修复此错误?
答案 0 :(得分:1)
我刚刚将您的代码粘贴到VS中并且它有效,但您应该注意两件事。 VB.net不关心变量的情况,因为它不区分大小写,因此control
与Control
相同,而另一个是根据MSDN Page on EM_SETCUEBANNER。
来自链接:
设置编辑控件显示的文本提示或提示,以提示用户输入信息。
参数 wParam [in]
如果提示横幅应该显示,即使编辑控件具有焦点,也为TRUE;否则,为假 FALSE是默认行为 - 当用户点击控件时,提示横幅消失。lParam [in]
指向Unicode字符串的指针,该字符串包含要显示为文本提示的文本。
如果你注意到我以粗体显示的部分,则说明当用户点击控件时它会消失,即控件具有焦点。如果您的测试程序中只有一个项目可以获得焦点,那么您将永远不会看到提示。
我的工作代码:
Imports System.Runtime.InteropServices
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
SetCueText(TextBox1, "Enter Name here")
End Sub
End Class
Public Module CueBannerText
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, <MarshalAs(UnmanagedType.LPWStr)> ByVal lParam As String) As Int32
End Function
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As IntPtr, ByVal hWnd2 As IntPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As IntPtr
Private Const EM_SETCUEBANNER As Integer = &H1501
Public Sub SetCueText(cntrl As Control, text As String)
If TypeOf cntrl Is ComboBox Then
Dim Edit_hWnd As IntPtr = FindWindowEx(cntrl.Handle, IntPtr.Zero, "Edit", Nothing)
If Not Edit_hWnd = IntPtr.Zero Then
SendMessage(Edit_hWnd, EM_SETCUEBANNER, 0, text)
End If
ElseIf TypeOf cntrl Is TextBox Then
SendMessage(cntrl.Handle, EM_SETCUEBANNER, 0, text)
End If
End Sub
End Module
答案 1 :(得分:0)
你有:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SetCueText(TextBox1, "Enter Name here")
End Sub
End Class
......哪个应该没问题,但是,你有:
Module CueBannerText
...
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SetCueText(TextBox1, "Enter Name here")
End Sub
End Module
您的错误很可能是由于Private Sub Form1_Load...
中的Module CueBannerText
造成的。由于您在Class Form1
中有此内容,因此模块中不需要它,因此请将其删除。
编辑:
另外,你有
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, <MarshalAs(UnmanagedType.LPWStr)> ByVal lParam As String) As Int32
End Function
...不需要End Function