Vb.net 创建一个文本框;提示“;两个不同文本框的消息(水印)

Vb.net 创建一个文本框;提示“;两个不同文本框的消息(水印),vb.net,winforms,Vb.net,Winforms,我想知道如何为表单中的两个不同文本框创建“提示” 以下是我的代码: 导入系统运行时互操作服务 表单加载事件: SendMessage(Me.txtAmount.Handle, &H1501, 0, "$X.XX") SendMessage(Me.txtMemo.Handle, &H1501, 0, "Enter a transaction memo.") <DllImport("user32.dll", CharSet:=CharSet.Auto)> _ P

我想知道如何为表单中的两个不同文本框创建“提示”

以下是我的代码:

导入系统运行时互操作服务

表单加载事件:

SendMessage(Me.txtAmount.Handle, &H1501, 0, "$X.XX")
SendMessage(Me.txtMemo.Handle, &H1501, 0, "Enter a transaction memo.")
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Private Shared 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

共享功能:

SendMessage(Me.txtAmount.Handle, &H1501, 0, "$X.XX")
SendMessage(Me.txtMemo.Handle, &H1501, 0, "Enter a transaction memo.")
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Private Shared 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
_
私有共享函数SendMessage(ByVal hWnd作为IntPtr,ByVal msg作为Integer,ByVal wParam作为Integer,ByVal lParam作为String)作为Int32
端函数
此代码仅适用于
txtAmount
文本框。你知道如何让两个人以一种形式工作吗

谢谢。

创建一个类:

Public Class MultilineTextBoxWaterMark
Inherits TextBox

Private Const WM_PAINT = &HF

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    MyBase.WndProc(m)

    If m.Msg = WM_PAINT Then
        If Text.Length <> 0 Or Me.Focused Then
            Return
        End If
        Using g As Graphics = Me.CreateGraphics, format As New StringFormat()
            format.LineAlignment = StringAlignment.Near

            g.DrawString("Enter a transaction memo.", Me.Font, Brushes.LightGray, Me.ClientRectangle, format)
        End Using
    End If
End Sub

End Class
我认为它有效。谢谢

创建一个类:

Public Class MultilineTextBoxWaterMark
Inherits TextBox

Private Const WM_PAINT = &HF

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    MyBase.WndProc(m)

    If m.Msg = WM_PAINT Then
        If Text.Length <> 0 Or Me.Focused Then
            Return
        End If
        Using g As Graphics = Me.CreateGraphics, format As New StringFormat()
            format.LineAlignment = StringAlignment.Near

            g.DrawString("Enter a transaction memo.", Me.Font, Brushes.LightGray, Me.ClientRectangle, format)
        End Using
    End If
End Sub

End Class

我认为它有效。谢谢

您可以在不使用API的情况下复制文本框提示:

''' <summary>
''' Indicates the Textbox Hint.
''' </summary>
Private ReadOnly TextBoxHint As String = "I'm a control hint."

''' <summary>
''' Handles the Hint event of the TextBox control.
''' </summary>
''' <param name="sender">The source of the event.</param>
''' <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
Private Sub TextBox_Leave(ByVal sender As Object, ByVal e As EventArgs) Handles _
TextBox1.Leave, TextBox1.HandleCreated

    Select Case CStr(sender.Text)

        Case Is = TextBoxHint
            sender.text = String.Empty

        Case Is = String.Empty
            sender.text = TextBoxHint

    End Select

End Sub

''' <summary>
''' Handles the Enter event of the TextBox control.
''' </summary>
''' <param name="sender">The source of the event.</param>
''' <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
Private Sub TextBox_Enter(ByVal sender As Object, ByVal e As EventArgs) Handles _
TextBox1.Enter

    Select Case CStr(sender.Text)

        Case Is = TextBoxHint
            sender.text = String.Empty

    End Select

End Sub
“”
''表示文本框提示。
''' 
Private ReadOnly TextBoxHint As String=“我是一个控件提示。”
''' 
''处理TextBox控件的提示事件。
''' 
''事件的源头。
''包含事件数据的实例。
私有子文本框_Leave(ByVal sender作为对象,ByVal e作为事件args)句柄_
TextBox1.离开,TextBox1.HandleCreated
选择案例CStr(sender.Text)
Case Is=TextBoxHint
sender.text=String.Empty
Case Is=String.Empty
sender.text=TextBoxHint
结束选择
端接头
''' 
''处理TextBox控件的Enter事件。
''' 
''事件的源头。
''包含事件数据的实例。
私有子文本框(ByVal sender作为对象,ByVal e作为EventArgs)句柄_
TextBox1.输入
选择案例CStr(sender.Text)
Case Is=TextBoxHint
sender.text=String.Empty
结束选择
端接头
无论如何,试试这个替代方案,我同时在各种文本框上进行了测试,效果很好:

' Set Control Hint
' ( By Elektro )
'
' Usage Examples:
' SetControlHint(TextBox1, "I'm a text hint.")

''' <summary>
''' Messages to send to an Edit control, such a TextBox.
''' </summary>
Private Enum EditControlMessages As Integer

    ''' <summary>
    ''' Sets the textual cue, or tip, that is displayed by the edit control to prompt the user for information.
    ''' http://msdn.microsoft.com/en-us/library/windows/desktop/bb761639%28v=vs.85%29.aspx
    ''' </summary>
    SetCueBanner = &H1501I

End Enum

''' <summary>
''' Sends the specified message to a window or windows. 
''' The SendMessage function calls the window procedure for the specified window 
''' and does not return until the window procedure has processed the message.
''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms644950%28v=vs.85%29.aspx
''' </summary>
''' <param name="hWnd">
''' A handle to the Control whose will receive the message.
''' </param>
''' <param name="msg">
''' The message to be sent.
''' </param>
''' <param name="wParam">
''' Additional message-specific information.
''' </param>
''' <param name="lParam">
''' Additional message-specific information.
''' </param>
''' <returns>
''' The return value specifies the result of the message processing; it depends on the message sent.
''' </returns>
<System.Runtime.InteropServices.DllImport("user32.dll",
EntryPoint:="SendMessage",
CharSet:=System.Runtime.InteropServices.CharSet.Auto)>
Private Shared Function SendEditMessage(
        ByVal hWnd As IntPtr,
        ByVal msg As EditControlMessages,
        ByVal wParam As IntPtr,
        <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)>
        ByVal lParam As String
) As UInteger
End Function

''' <summary>
''' Sets a text hint for an edit control such a TextBox.
''' </summary>
''' <param name="Control">Indicates the control.</param>
''' <param name="Hint">Indicates the text hint.</param>
''' <returns><c>true</c> if operation succeeds, <c>false</c> otherwise.</returns>
Private Function SetControlHint(ByVal [Control] As Control,
                                ByVal Hint As String) As Boolean

    Return SendEditMessage([Control].Handle, EditControlMessages.SetCueBanner, IntPtr.Zero, Hint)

End Function
”设置控制提示
"(Elektro)
'
'用法示例:
'SetControlHint(TextBox1,“我是一个文本提示”)
''' 
''消息发送到编辑控件,如文本框。
''' 
私有枚举EditControlMessages作为整数
''' 
''设置编辑控件显示的文本提示或提示,以提示用户输入信息。
''' http://msdn.microsoft.com/en-us/library/windows/desktop/bb761639%28v=vs.85%29.aspx
''' 
SetCueBanner=&H1501I
结束枚举
''' 
''将指定的消息发送到一个或多个窗口。
''SendMessage函数调用指定窗口的窗口过程
'',直到窗口过程处理完消息后才返回。
''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms644950%28v=vs.85%29.aspx
''' 
''' 
''将接收消息的控件的句柄。
''' 
''' 
''要发送的消息。
''' 
''' 
''其他消息特定信息。
''' 
''' 
''其他消息特定信息。
''' 
''' 
''返回值指定消息处理的结果;这取决于发送的消息。
''' 
私有共享函数SendEditMessage(
ByVal hWnd作为IntPtr,
ByVal msg作为EditControlMessages,
ByVal wParam作为IntPtr,
作为字符串的ByVal lParam
)作为尤因泰格
端函数
''' 
''为编辑控件(如文本框)设置文本提示。
''' 
''表示控件。
''表示文本提示。
''如果操作成功,则为true,否则为false。
私有函数SetControlHint(ByVal[Control]作为控件,
ByVal提示(作为字符串)作为布尔值
返回SendEditMessage([Control].Handle,EditControlMessages.SetCueBanner,IntPtr.Zero,Hint)
端函数

您可以在不使用API的情况下复制文本框提示:

''' <summary>
''' Indicates the Textbox Hint.
''' </summary>
Private ReadOnly TextBoxHint As String = "I'm a control hint."

''' <summary>
''' Handles the Hint event of the TextBox control.
''' </summary>
''' <param name="sender">The source of the event.</param>
''' <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
Private Sub TextBox_Leave(ByVal sender As Object, ByVal e As EventArgs) Handles _
TextBox1.Leave, TextBox1.HandleCreated

    Select Case CStr(sender.Text)

        Case Is = TextBoxHint
            sender.text = String.Empty

        Case Is = String.Empty
            sender.text = TextBoxHint

    End Select

End Sub

''' <summary>
''' Handles the Enter event of the TextBox control.
''' </summary>
''' <param name="sender">The source of the event.</param>
''' <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
Private Sub TextBox_Enter(ByVal sender As Object, ByVal e As EventArgs) Handles _
TextBox1.Enter

    Select Case CStr(sender.Text)

        Case Is = TextBoxHint
            sender.text = String.Empty

    End Select

End Sub
“”
''表示文本框提示。
''' 
Private ReadOnly TextBoxHint As String=“我是一个控件提示。”
''' 
''处理TextBox控件的提示事件。
''' 
''事件的源头。
''包含事件数据的实例。
私有子文本框_Leave(ByVal sender作为对象,ByVal e作为事件args)句柄_
TextBox1.离开,TextBox1.HandleCreated
选择案例CStr(sender.Text)
Case Is=TextBoxHint
sender.text=String.Empty
Case Is=String.Empty
sender.text=TextBoxHint
结束选择
端接头
''' 
''处理TextBox控件的Enter事件。
''' 
''事件的源头。
''包含事件数据的实例。
私有子文本框(ByVal sender作为对象,ByVal e作为EventArgs)句柄_
TextBox1.输入
选择案例CStr(sender.Text)
Case Is=TextBoxHint
sender.text=String.Empty
结束选择
端接头
无论如何,试试这个替代方案,我同时在各种文本框上进行了测试,效果很好:

' Set Control Hint
' ( By Elektro )
'
' Usage Examples:
' SetControlHint(TextBox1, "I'm a text hint.")

''' <summary>
''' Messages to send to an Edit control, such a TextBox.
''' </summary>
Private Enum EditControlMessages As Integer

    ''' <summary>
    ''' Sets the textual cue, or tip, that is displayed by the edit control to prompt the user for information.
    ''' http://msdn.microsoft.com/en-us/library/windows/desktop/bb761639%28v=vs.85%29.aspx
    ''' </summary>
    SetCueBanner = &H1501I

End Enum

''' <summary>
''' Sends the specified message to a window or windows. 
''' The SendMessage function calls the window procedure for the specified window 
''' and does not return until the window procedure has processed the message.
''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms644950%28v=vs.85%29.aspx
''' </summary>
''' <param name="hWnd">
''' A handle to the Control whose will receive the message.
''' </param>
''' <param name="msg">
''' The message to be sent.
''' </param>
''' <param name="wParam">
''' Additional message-specific information.
''' </param>
''' <param name="lParam">
''' Additional message-specific information.
''' </param>
''' <returns>
''' The return value specifies the result of the message processing; it depends on the message sent.
''' </returns>
<System.Runtime.InteropServices.DllImport("user32.dll",
EntryPoint:="SendMessage",
CharSet:=System.Runtime.InteropServices.CharSet.Auto)>
Private Shared Function SendEditMessage(
        ByVal hWnd As IntPtr,
        ByVal msg As EditControlMessages,
        ByVal wParam As IntPtr,
        <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)>
        ByVal lParam As String
) As UInteger
End Function

''' <summary>
''' Sets a text hint for an edit control such a TextBox.
''' </summary>
''' <param name="Control">Indicates the control.</param>
''' <param name="Hint">Indicates the text hint.</param>
''' <returns><c>true</c> if operation succeeds, <c>false</c> otherwise.</returns>
Private Function SetControlHint(ByVal [Control] As Control,
                                ByVal Hint As String) As Boolean

    Return SendEditMessage([Control].Handle, EditControlMessages.SetCueBanner, IntPtr.Zero, Hint)

End Function
”设置控制提示
"(Elektro)
'
'用法示例:
'SetControlHint(TextBox1,“我是一个文本提示”)
''' 
''消息发送到编辑控件,如文本框。
''' 
私有枚举EditControlMessages作为整数
''' 
''设置编辑控件显示的文本提示或提示,以提示用户输入信息。
''' http://msdn.microsoft.com/en-us/library/windows/desktop/bb761639%28v=vs.85%29.aspx
''' 
SetCueBanner=&H1501I
结束枚举
''' 
''将指定的消息发送到一个或多个窗口。
''SendMessage函数调用指定窗口的窗口过程
'',直到窗口过程处理完消息后才返回。
''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms644950%28v=vs.85%29.aspx
''' 
''' 
''将接收消息的控件的句柄。
''' 
''' 
''要发送的消息。
''' 
''' 
''其他消息特定信息。
''' 
''' 
''其他消息特定信息。
''' 
''' 
''返回值指定消息处理的结果;这取决于发送的消息。
''' 
私有共享函数SendEditMessage(
ByVal hWnd作为IntPtr,
ByVal msg作为EditControlMessages,
ByVal wParam作为IntPtr,
作为字符串的ByVal lParam
)作为