Window 表单中的文本框占位符使用 VB.NET

Placeholder in TextBox in Window Forms using VB.NET

我正在 VB.NET 开发一个 Windows 表单应用程序,我目前正在使用标签和文本框制作登录屏幕。

我需要的是 TextBox 控件中的 Placeholder 你可以在下面看到 ↓ (显然不是我的)

TextBox 中是否有任何 属性 允许我将默认占位符(占位符、水印、提示、提示)设置为我想要的? 如果没有,我该如何解决这个问题?

尝试给出的答案 here

编辑: 我正在通过添加代码和规范来编辑答案

使用下面的代码,只替换文本框的名称和要显示为占位符的文本,因为您的代码中有它们,我有将它们命名为 TextBox1 和 "User Name" => 用于用户名 texbox 和 TextBox2 以及 "Password" => 用于密码文本框并确保更改它们

Private Sub TextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles TextBox1.Enter, TextBox2.Enter

    Dim tb As TextBox = DirectCast(sender, TextBox)

    With tb
        .ForeColor = Color.Black
        .Font = New Font("Microsoft Sans Serif", 8, FontStyle.Regular)

        Select Case tb.Name
            Case "TextBox1"
                If .Text = "User Name" Then
                    .Clear()
                End If

            Case "TextBox2"
                If .Text = "Password" Then
                    .Clear()
                End If
        End Select
    End With

End Sub

Private Sub TextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles TextBox1.Leave, TextBox2.Leave

    Dim tb As TextBox = DirectCast(sender, TextBox)

    If tb.Text.Trim = "" Then
        Select Case tb.Name
            Case "TextBox1"
                SetUserToDefault()

            Case "TextBox2"
                SetPasswordToDefault()
        End Select
    End If

End Sub

Private Sub SetUserToDefault()

    With TextBox1
        .ForeColor = Color.Gray
        .Font = New Font("Arial", 10, FontStyle.Italic)
        .Text = "User Name"
        .TabStop = False
    End With

End Sub

Private Sub SetPasswordToDefault()

    With TextBox2
        .ForeColor = Color.Gray
        .Font = New Font("Arial", 10, FontStyle.Italic)
        .Text = "Password"
        .TabStop = False
    End With
End Sub

我编译并尝试了代码并且它有效,所以如果它仍然无效,请在此处提供示例。

此致

此功能不是内置的。但是,您可以创建一个继承自 TextBox 的控件并在您的表单上使用它们。这是我之前制作的:Placeholder textbox

主要特点如下。有一个 属性 用于 Placeholder 文本和一个 bool 属性 计算是否显示它:

public string Placeholder { get; set; }
public bool HasUserContent
{
    get
    {
        return (!string.IsNullOrEmpty(Text) && Text != Placeholder);
    }
}

在实例化期间,将事件处理程序设置为焦点和模糊:

GotFocus += PlaceholderTextbox_GotFocus;
LostFocus += PlaceholderTextbox_LostFocus;

并实现这些事件以在灰色占位符文本和黑色用户文本之间切换:

private void PlaceholderTextbox_LostFocus(object sender, EventArgs e)
{
    if (Text == string.Empty)
    {
        Text = Placeholder;
        ForeColor = SystemColors.GrayText;
    }
}

private void PlaceholderTextbox_GotFocus(object sender, EventArgs e)
{
    if (Text == Placeholder)
    {
        Text = string.Empty;
        ForeColor = SystemColors.ControlText;
    }
}

我在上面链接的文件中有更多详细信息。 希望这对您有所帮助!

.NET 5.0+ 或 .NET Core 3.0+

使用TextBox.PlaceholderText 属性:

textBox1.PlaceholderText = "Enter your name"

.NET 框架

您可以使用以下任一方法:

  • 发送EM_SETCUEBANNER使用TextBox的内置占位符功能(仅支持带灰色占位符文本的单行文本)

  • 处理 WM_PAINT 消息以在多行和单行文本框上显示具有自定义颜色的占位符(这是后来在 .NET Core 中实现的方式)

使用EM_SETCUEBANNER

You can find a C# implementation of this approach here in this post.

通过将 EM_SETCUEBANNER 发送到 TextBox,您可以设置编辑控件显示的文本提示或提示,以提示用户输入信息。

Imports System
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Public Class MyTextBox
    Inherits TextBox

    Private Const EM_SETCUEBANNER As Integer = &H1501
    <DllImport("user32.dll", CharSet:=CharSet.Auto)>
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, _
        ByVal wParam As Integer, ByVal lParam As String) As Int32
    End Function

    Protected Overrides Sub OnHandleCreated(e As EventArgs)
        MyBase.OnHandleCreated(e)
        If Not String.IsNullOrEmpty(CueBanner) Then UpdateCueBanner()
    End Sub

    Private m_CueBanner As String
    Public Property CueBanner As String
        Get
            Return m_CueBanner
        End Get
        Set(ByVal value As String)
            m_CueBanner = value
            UpdateCueBanner()
        End Set
    End Property

    Private Sub UpdateCueBanner()
        SendMessage(Me.Handle, EM_SETCUEBANNER, 0, CueBanner)
    End Sub
End Class

处理WM_PAINT

You can find a C# implementation of this approach here in this post.

如果您使用EM_SETCUEBANNER,提示将始终以系统默认颜色显示。此外,当 TextBox 为 MultiLine 时,将不会显示提示。

使用绘画解决方案,您可以用您想要的任何颜色显示文本。多行控件也可以显示水印

Imports System.Drawing
Imports System.Windows.Forms
Public Class ExTextBox
    Inherits TextBox

    Private m_Hint As String
    Public Property Hint As String
        Get
            Return m_Hint
        End Get
        Set(ByVal value As String)
            m_Hint = value
            Me.Invalidate()
        End Set
    End Property

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

        If m.Msg = &HF Then
            If Not Me.Focused AndAlso String.IsNullOrEmpty(Me.Text) _
                AndAlso Not String.IsNullOrEmpty(Me.Hint) Then
                Using g = Me.CreateGraphics()
                    TextRenderer.DrawText(g, Me.Hint, Me.Font, Me.ClientRectangle, _
                        SystemColors.GrayText, Me.BackColor, _
                        TextFormatFlags.Top Or TextFormatFlags.Left)
                End Using
            End If
        End If
    End Sub
End Class