1

我在网上找到了以下代码:

 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.

有谁知道如何实现一个有效的提示横幅,或者如何解决这个错误?

4

2 回答 2

2

我刚刚将您的代码粘贴到 VS 中并且它可以工作,但是您应该注意两件事。VB.net 不关心变量的大小写,因为它不区分大小写,因此与EM_SETCUEBANNER 上的 MSDN 页面control相同,Control另一件事是。

来自链接:

设置由编辑控件显示的文本提示或提示,以提示用户输入信息。

参数 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
于 2013-09-01T23:56:42.517 回答
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...in 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

于 2013-09-01T22:30:54.703 回答