vb.net 如何在 VB .NET 中设置 TextBox 提示标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7036028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:38:56  来源:igfitidea点击:

How to set TextBox hint label in VB .NET?

vb.net

提问by Royal Pinto

In my application I have one text box for entering user name. If text is empty i want to show "Enter User name here" in same text box in gray color. Is there any property like this for text box. Like in Firefox browser if URL field is empty it will show "Go to a web site" In gray color

在我的应用程序中,我有一个用于输入用户名的文本框。如果文本为空,我想在同一文本框中以灰色显示“在此处输入用户名”。文本框是否有这样的属性。就像在 Firefox 浏览器中一样,如果 URL 字段为空,它将以灰色显示“转到网站”

Thanks

谢谢

回答by Grahamvs

I know this may be an old thread, but I thought I'd answer it just in case anyone else stumbled across it.

我知道这可能是一个旧线程,但我想我会回答它,以防其他人偶然发现它。

First declare the following (you may need to import System.Runtime.InteropServices)

首先声明以下内容(您可能需要导入System.Runtime.InteropServices

<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

Then call the following (change as needed):

然后调用以下(根据需要更改):

SendMessage(Me.textbox1.Handle, &H1501, 0, "Enter User name here")

回答by Dan Abramov

Assuming you mean Windows Forms, take a look at this question.
Basically, you need to call a WinAPI SendMessagefunction for the control with EM_SETCUEBANNERvalue.

假设您的意思是 Windows 窗体,请查看此问题
基本上,您需要为SendMessage带有EM_SETCUEBANNER值的控件调用 WinAPI函数。

回答by nlinus

I really like this solution from CodeProject.com: http://www.codeproject.com/KB/edit/TextBoxHint.aspx?display=Print

我真的很喜欢 CodeProject.com 的这个解决方案:http: //www.codeproject.com/KB/edit/TextBoxHint.aspx?display= Print

What's really nice is the slick fade out as the user types in his/her text into the field. It's pretty darn easy to implement and looks great.

当用户在字段中输入他/她的文本时,光滑的淡出效果非常好。它非常容易实现并且看起来很棒。

回答by Ivan Ferrer Villa

you can subclass TextBox and override WndProc

您可以继承 TextBox 并覆盖 WndProc

Public Class TextBoxPlaceHolder
    Inherits TextBox
    Private Const WM_PAINT As Int32 = &HF
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)
        If m.Msg = WM_PAINT AndAlso Me.TextLength = 0 Then
            Using g = Me.CreateGraphics
                g.DrawString("Enter User name here", Me.Font, Brushes.Gray, 1, 1)
            End Using
        End If
    End Sub
End Class

回答by Isidoros Moulas

Awesome work Ivan and cardmagik. Create a usercontrol and use the below code.

很棒的工作 Ivan 和 cardmagik。创建一个用户控件并使用以下代码。

Public Class RichTextBoxPlaceHolder
    Inherits RichTextBox
    Private Const WM_PAINT As Int32 = &HF
    Private mstrHint As String = "Enter text"

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)
        If m.Msg = WM_PAINT AndAlso Me.TextLength = 0 Then
            Using g = Me.CreateGraphics
                g.DrawString(mstrHint, Me.Font, Brushes.Gray, 1, 1)
            End Using
        End If
    End Sub

    Public Property Hint() As String
        Get
            Return mstrHint
        End Get
        Set(ByVal value As String)
            mstrHint = value
        End Set
    End Property

End Class

Screenshot:

截屏:

enter image description here

在此处输入图片说明

回答by Jared Ng

Try something along the lines of this:

尝试一些类似的东西:

(onkeyup)
If TextBox.Text = "" Then
  TextBox.Text = "Enter username here..."
  TextBox.ForeColor = <your chosen color here>
Else 
  TextBox.ForeColor = <normal color here>
End If