多个文本框上的占位符

Placeholder on multiple textboxes

我正在使用以下函数在文本框上创建占位符效果并且工作正常。

现在,我想在多个文本框上使用它。因为我只有一个盒子,所以 placeholder 变量就足够了。对于多个框,我需要多个占位符字符串。有没有办法将自定义 属性 添加到文本框,以便我可以从占位符函数访问它?

public MainForm()
{
    InitializeComponent();
    Placeholder_Show(DomainBox, null);
}

public static string placeholder = "example.com";

private void Placeholder_Hide(object sender, EventArgs e)
{
    var box = sender as TextBox;
    if (box.Text == placeholder)
    {
        box.Text = "";
        box.ForeColor = Color.Black;
        box.Font = new Font("Segoe UI", 10.2F, FontStyle.Regular);
    }
}

private void Placeholder_Show(object sender, EventArgs e)
{
    var box = sender as TextBox;
    if (string.IsNullOrEmpty(box.Text))
    {
        box.Text = placeholder;
        box.ForeColor = Color.Gray;
        box.Font = new Font("Segoe UI", 10.2F, FontStyle.Italic);
    }
}

所需代码(示例):

textBox1.placeholder = "some";
textBox2.placeholder = "string";

private void Placeholder_Hide(object sender, EventArgs e)
{
    var box = sender as TextBox;
    if (box.Text == box.placeholder) // placeholders are associated with boxes
    {
        box.Text = "";
        box.ForeColor = Color.Black;
        box.Font = new Font("Segoe UI", 10.2F, FontStyle.Regular);
    }
}

private void Placeholder_Show(object sender, EventArgs e)
{
    var box = sender as TextBox;
    if (string.IsNullOrEmpty(box.Text))
    {
        box.Text = box.placeholder;
        box.ForeColor = Color.Gray;
        box.Font = new Font("Segoe UI", 10.2F, FontStyle.Italic);
    }
}

我对 C# 不是很熟悉。也许,有更好的方法来处理这个问题。

如评论中所述,您可以子类化 Textbox 并添加该功能。这可能是总体上最好的选择,但是如果您想使用几个这样不相关的调整怎么办?另一种选择是以类似于 ErrorProvider 的方式实现它。您会注意到该组件向窗体上的其他控件添加了 属性。这是通过 extender provider.

完成的

经过一些研究,我注意到您可以创建自定义控件。我决定创建一个具有占位符效果的自定义文本框,这样下次我需要这样的东西时就不用费心了。

添加了一个新的 class 文件:CustomControls.cs

using System;
using System.Drawing;
using System.Windows.Forms;

namespace CustomControls
{
    public class CustomTextBox : TextBox
    {
        private string _placeholder;

        public string Placeholder
        {
            get { return this._placeholder; }
            set {
                this._placeholder = value;
                this.Placeholder_Show(null, null);
            }
        }

        public CustomTextBox()
        {
            Initialize();
        }

        private void Initialize()
        {
            this.Enter += new EventHandler(this.Placeholder_Hide);
            this.Leave += new EventHandler(this.Placeholder_Show);
        }

        private void Placeholder_Hide(object sender, EventArgs e)
        {
            if (this.Text == this._placeholder)
            {
                this.Text = "";
                this.ForeColor = Color.Black;
                this.Font = new Font("Segoe UI", 10.2F, FontStyle.Regular);
            }
        }

        private void Placeholder_Show(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.Text))
            {
                this.Text = this._placeholder;
                this.ForeColor = Color.Gray;
                this.Font = new Font("Segoe UI", 10.2F, FontStyle.Italic);
            }
        }
    }
}

编辑了设计器文件:MainForm.Designer.cs(不想创建新的文本框)

已更改

this.DomainBox = new System.Windows.Forms.TextBox();

进入

this.DomainBox = new CustomControls.CustomTextBox();

现在,这个新的 CustomTextBox 出现在工具箱顶部的名称“[namespace] Components”下。您可以像拖放普通文本框一样拖放它。此外,您可以在名称 Misc.

下的属性 window 中访问 Placeholder 属性