为两个不同的文本框(水印)创建文本框“提示”消息

时间:2014-02-18 19:09:54

标签: vb.net winforms

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

这是我的代码:

Imports System.Runtime.InteropServices

Form_Load事件:

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

此代码仅适用于txtAmount文本框。知道如何让两个人以一种形式工作吗?

感谢。

3 个答案:

答案 0 :(得分:6)

创建一个类:

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

然后以您的主要形式:

Private MltLnTxtBxWrMrk As New MultilineTextBoxWaterMark

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    MltLnTxtBxWrMrk.Location = New Point(100, 30) 'whatever you want
    MltLnTxtBxWrMrk.Width = 300 'whatever you want
    MltLnTxtBxWrMrk.Height = 100 'whatever you want
    MltLnTxtBxWrMrk.BorderStyle = BorderStyle.Fixed3D 'whatever you want
    MltLnTxtBxWrMrk.Multiline = True

    Me.Controls.Add(MltLnTxtBxWrMrk)
End Sub

我认为它有效。谢谢

答案 1 :(得分:1)

您可以在没有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

无论如何,尝试这个替代方案,我已经在各种文本框上同时测试并且工作得很好:

' 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

答案 2 :(得分:0)

刚刚遇到这个。 除了user3311691发布之外,我建议添加几行来提高呈现文本的质量:

Using g As Graphics = Me.CreateGraphics, oFmt As New StringFormat()
            oFmt.LineAlignment = StringAlignment.Near
            g.Clear(SystemColors.Window)
            g.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
            g.DrawString(CueBannerText, Me.Font, SystemBrushes.GrayText, Me.ClientRectangle, oFmt)
        End Using