user2087008
user2087008

Reputation:

Implementing a Cue Banner to a textbox

I have found the following code on the internet:

 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

However it gives me the following error:

'Handles' in modules must specify a 'WithEvents' variable qualified with a single identifier.

Does anybody know how to implement a working cue banner, or how to fix this error?

Upvotes: 1

Views: 4340

Answers (2)

Mark Hall
Mark Hall

Reputation: 54552

I have just pasted your code into VS and it works, there are 2 things you should be aware of though. VB.net does not care about the case of your variables since it is case insensitive therefore control is the same as Control and the other thing is that according to the MSDN Page on EM_SETCUEBANNER .

From Link:

Sets the textual cue, or tip, that is displayed by the edit control to prompt the user for information.

Parameters wParam [in]
TRUE if the cue banner should show even when the edit control has focus; otherwise, FALSE.
FALSE is the default behavior—the cue banner disappears when the user clicks in the control.

lParam [in]
A pointer to a Unicode string that contains the text to display as the textual cue.

if you note the part that I put in bold it states that it will disappear when the user clicks in the control, i.e. the control has focus. There for if you just have one item on your test program that can receive focus you will never see the cue.

My Working Code:

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

Upvotes: 2

Monty Wild
Monty Wild

Reputation: 4001

You have:

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

...Which should be fine, however, you then have:

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

Your error is most likely due to the Private Sub Form1_Load... in Module CueBannerText. Since you have that in your Class Form1, it isn't needed in the module, so delete it.

EDIT:

Also, you have

<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

...which doesn't need the End Function

Upvotes: 0

Related Questions