将提示横幅实现到文本框 [英] Implementing a Cue Banner to a textbox

查看:18
本文介绍了将提示横幅实现到文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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 .

来自链接:

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

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

参数wParam [输入]
如果即使编辑控件具有焦点也应显示提示横幅,则为 TRUE;否则,为假.
FALSE 是默认行为——当用户点击控件时提示横幅消失.

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 [输入]
指向包含要显示为文本提示的文本的 Unicode 字符串的指针.

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.

我的工作代码:

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

这篇关于将提示横幅实现到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆