Watermark TextBox in WinForms

前端 未结 10 1849
说谎
说谎 2020-11-22 00:35

Can anyone point me to a good implementation of a basic Windows Forms TextBox that will initially show watermark text that disappears when the cursor enters it? I think I ca

10条回答
  •  情话喂你
    2020-11-22 01:08

    I wrote a reusable custom control class for my project.
    maybe it can help someone that need to implement multiple placeholder textboxes in his project. here is the C# and vb.net versions:

    C#:

    namespace reusebleplaceholdertextbox
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                // implementation
                CustomPlaceHolderTextbox myCustomTxt = new CustomPlaceHolderTextbox(
                    "Please Write Text Here...", Color.Gray, new Font("ARIAL", 11, FontStyle.Italic)
                    , Color.Black, new Font("ARIAL", 11, FontStyle.Regular)
                    );
    
                myCustomTxt.Multiline = true;
                myCustomTxt.Size = new Size(200, 50);
                myCustomTxt.Location = new Point(10, 10);
                this.Controls.Add(myCustomTxt);
            }
        }
    
        class CustomPlaceHolderTextbox : System.Windows.Forms.TextBox
        {
            public string PlaceholderText { get; private set; }
            public Color PlaceholderForeColor { get; private set; }
            public Font PlaceholderFont { get; private set; }
    
            public Color TextForeColor { get; private set; }
            public Font TextFont { get; private set; }
    
            public CustomPlaceHolderTextbox(string placeholdertext, Color placeholderforecolor,
                Font placeholderfont, Color textforecolor, Font textfont)
            {
                this.PlaceholderText = placeholdertext;
                this.PlaceholderFont = placeholderfont;
                this.PlaceholderForeColor = placeholderforecolor;
                this.PlaceholderFont = placeholderfont;
                this.TextForeColor = textforecolor;
                this.TextFont = textfont;
                if (!string.IsNullOrEmpty(this.PlaceholderText))
                {
                    SetPlaceHolder(true);
                    this.Update();
                }
            }
    
            private void SetPlaceHolder(bool addEvents)
            {
                if (addEvents)
                {  
                    this.LostFocus += txt_lostfocus;
                    this.Click += txt_click;
                }
    
                this.Text = PlaceholderText;
                this.ForeColor = PlaceholderForeColor;
                this.Font = PlaceholderFont;
            }
    
            private void txt_click(object sender, EventArgs e)
            {
                // IsNotFirstClickOnThis:
                // if there is no other control in the form
                // we will have a problem after the first load
                // because we dont other focusable control to move the focus to
                // and we dont want to remove the place holder
                // only on first time the place holder will be removed by click event
                RemovePlaceHolder();
                this.GotFocus += txt_focus;
                // no need for this event listener now
                this.Click -= txt_click;
            }
    
            private void RemovePlaceHolder()
            {
                this.Text = "";
                this.ForeColor = TextForeColor;
                this.Font = TextFont;
            }
            private void txt_lostfocus(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(this.Text))
                {
                    // set placeholder again
                    SetPlaceHolder(false);
                }
            }
    
            private void txt_focus(object sender, EventArgs e)
            {
                if (this.Text == PlaceholderText)
                {
                    // IsNotFirstClickOnThis:
                    // if there is no other control in the form
                    // we will have a problem after the first load
                    // because we dont other focusable control to move the focus to
                    // and we dont want to remove the place holder
                    RemovePlaceHolder();
                }
            }
        }
    }
    


    VB.NET:


    Namespace CustomControls
    
        Public Class PlaceHolderTextBox
            Inherits System.Windows.Forms.TextBox
    
            Public Property PlaceholderText As String
            Public Property PlaceholderForeColor As Color
            Public Property PlaceholderFont As Font
            Public Property TextForeColor As Color
            Public Property TextFont As Font
    
            Public Sub New(ByVal placeholdertext As String, ByVal placeholderforecolor As Color, ByVal placeholderfont As Font, ByVal txtboxbackcolor As Color, ByVal textforecolor As Color, ByVal textfont As Font)
                Me.PlaceholderText = placeholdertext
                Me.PlaceholderFont = placeholderfont
                Me.PlaceholderForeColor = placeholderforecolor
                Me.PlaceholderFont = placeholderfont
                Me.TextForeColor = textforecolor
                Me.TextFont = textfont
                Me.BackColor = txtboxbackcolor
                If Not String.IsNullOrEmpty(Me.PlaceholderText) Then
                    SetPlaceHolder(True)
                    Me.Update()
                End If
            End Sub
    
            Private Sub SetPlaceHolder(ByVal addEvents As Boolean)
                If addEvents Then
                    AddHandler Me.LostFocus, AddressOf txt_lostfocus
                    AddHandler Me.Click, AddressOf txt_click
                End If
    
                Me.Text = PlaceholderText
                Me.ForeColor = PlaceholderForeColor
                Me.Font = PlaceholderFont
            End Sub
    
            Private Sub txt_click(ByVal sender As Object, ByVal e As EventArgs)
                RemovePlaceHolder()
                AddHandler Me.GotFocus, AddressOf txt_focus
                RemoveHandler Me.Click, AddressOf txt_click
            End Sub
    
            Private Sub RemovePlaceHolder()
                Me.Text = ""
                Me.ForeColor = TextForeColor
                Me.Font = TextFont
            End Sub
    
            Private Sub txt_lostfocus(ByVal sender As Object, ByVal e As EventArgs)
                If String.IsNullOrEmpty(Me.Text) Then
                    SetPlaceHolder(False)
                End If
            End Sub
    
            Private Sub txt_focus(ByVal sender As Object, ByVal e As EventArgs)
                If Me.Text = PlaceholderText Then
                    RemovePlaceHolder()
                End If
            End Sub
        End Class
    
    End Namespace
    

提交回复
热议问题