1. <i id='3Xh6b'><tr id='3Xh6b'><dt id='3Xh6b'><q id='3Xh6b'><span id='3Xh6b'><b id='3Xh6b'><form id='3Xh6b'><ins id='3Xh6b'></ins><ul id='3Xh6b'></ul><sub id='3Xh6b'></sub></form><legend id='3Xh6b'></legend><bdo id='3Xh6b'><pre id='3Xh6b'><center id='3Xh6b'></center></pre></bdo></b><th id='3Xh6b'></th></span></q></dt></tr></i><div id='3Xh6b'><tfoot id='3Xh6b'></tfoot><dl id='3Xh6b'><fieldset id='3Xh6b'></fieldset></dl></div>
    2. <small id='3Xh6b'></small><noframes id='3Xh6b'>

        <bdo id='3Xh6b'></bdo><ul id='3Xh6b'></ul>

    3. <legend id='3Xh6b'><style id='3Xh6b'><dir id='3Xh6b'><q id='3Xh6b'></q></dir></style></legend>

        <tfoot id='3Xh6b'></tfoot>

        使用 VB.NET 在窗口窗体中的 TextBox 中的占位符

        时间:2023-06-09

          <tbody id='3hRYZ'></tbody>
        • <legend id='3hRYZ'><style id='3hRYZ'><dir id='3hRYZ'><q id='3hRYZ'></q></dir></style></legend>

            <small id='3hRYZ'></small><noframes id='3hRYZ'>

              <tfoot id='3hRYZ'></tfoot>
            • <i id='3hRYZ'><tr id='3hRYZ'><dt id='3hRYZ'><q id='3hRYZ'><span id='3hRYZ'><b id='3hRYZ'><form id='3hRYZ'><ins id='3hRYZ'></ins><ul id='3hRYZ'></ul><sub id='3hRYZ'></sub></form><legend id='3hRYZ'></legend><bdo id='3hRYZ'><pre id='3hRYZ'><center id='3hRYZ'></center></pre></bdo></b><th id='3hRYZ'></th></span></q></dt></tr></i><div id='3hRYZ'><tfoot id='3hRYZ'></tfoot><dl id='3hRYZ'><fieldset id='3hRYZ'></fieldset></dl></div>

                <bdo id='3hRYZ'></bdo><ul id='3hRYZ'></ul>
                1. 本文介绍了使用 VB.NET 在窗口窗体中的 TextBox 中的占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  I'm working on a Windows Forms application in VB.NET and I am currently making a login screen using labels and TextBoxes.

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

                  What I need is the Placeholder in TextBox controls you can see below ↓ (not mine obviously)

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

                  Is there any property in TextBox that allows me to set the default placeholder (placeholder, watermark, hint, tip) to what I want it to be? If there is not any, how can i solve this differently?

                  推荐答案

                  使用 EM_SETCUEBANNER

                  您可以在 this 中找到此方法的 C# 实现发帖.

                  通过发送 EM_SETCUEBANNERTextBox,您可以设置由编辑控件显示的文本提示或提示,以提示用户输入信息.

                  By sending EM_SETCUEBANNER to a TextBox, you can set the textual cue, or tip, that is displayed by the edit control to prompt the user for information.

                  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

                  您可以在 这里找到此方法的 C# 实现发帖.

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

                  If you use EM_SETCUEBANNER, the hint always will be shown in a system default color. Also the hint will not be shown when the TextBox is MultiLine.

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

                  Using the painting solution, you can show the text with any color that you want. You also can show the watermark when the control is multi-line

                  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
                  

                  这篇关于使用 VB.NET 在窗口窗体中的 TextBox 中的占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何绑定到 CaretIndex 又名文本框的光标位置 下一篇:在文本框控件内添加标签

                  相关文章

                2. <legend id='7u1xL'><style id='7u1xL'><dir id='7u1xL'><q id='7u1xL'></q></dir></style></legend>

                  1. <tfoot id='7u1xL'></tfoot>

                        <bdo id='7u1xL'></bdo><ul id='7u1xL'></ul>

                    1. <small id='7u1xL'></small><noframes id='7u1xL'>

                      <i id='7u1xL'><tr id='7u1xL'><dt id='7u1xL'><q id='7u1xL'><span id='7u1xL'><b id='7u1xL'><form id='7u1xL'><ins id='7u1xL'></ins><ul id='7u1xL'></ul><sub id='7u1xL'></sub></form><legend id='7u1xL'></legend><bdo id='7u1xL'><pre id='7u1xL'><center id='7u1xL'></center></pre></bdo></b><th id='7u1xL'></th></span></q></dt></tr></i><div id='7u1xL'><tfoot id='7u1xL'></tfoot><dl id='7u1xL'><fieldset id='7u1xL'></fieldset></dl></div>