.net - 使用 VB.NET 窗口窗体中文本框中的占位符

标签 .net vb.net winforms textbox

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

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

Prototype image taken from google pictures

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

最佳答案

.NET 5.0+ 或 .NET Core 3.0+

使用TextBox.PlaceholderText属性:

textBox1.PlaceholderText = "Enter your name"

.NET 框架

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

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

  • 处理WM_PAINT在多行和单行 TextBox 上显示具有自定义颜色的占位符的消息(这是稍后在 .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_SETCUEBANER,提示将始终以系统默认颜色显示。当 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

关于.net - 使用 VB.NET 窗口窗体中文本框中的占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50924572/

相关文章:

用于编辑 XML/XSL 的 .NET Windows 控件?

vb.net - 数组或列表中的项目是否保持其顺序?

c# - 需要一个通过 USB 端口控制的 Switch 组件

vb.net - Windows服务: How to trigger OnStop when the worker thread stops

winforms - 请求的剪贴板操作未成功

c# - 枚举 "Inheritance"

.net - 与 Tortoise SVN 的持续集成

.net - Fluent NHibernate - 仅当不存在时才创建数据库模式

c# - 如何以编程方式使用 C# 从 BitBucket 中提取代码

c# - 在 RichTextBox 中用空格链接到文件的路径?