【问题标题】:Placeholder on multiple textboxes多个文本框上的占位符
【发布时间】:2017-11-29 16:17:58
【问题描述】:

我正在使用以下函数在文本框上创建占位符效果,并且效果很好。

现在,我想在多个文本框上使用它。因为我只有一个盒子,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# 不是很熟悉。也许,有更好的方法来解决这个问题。

【问题讨论】:

标签: c# textbox placeholder


【解决方案1】:

如 cmets 中所述,您可以将 Textbox 子类化并添加该功能。一般来说,这可能是最好的选择,但是如果你想使用几个这样的不相关的调整呢?另一种选择是以与ErrorProvider 类似的方式实现它。您会注意到该组件向窗体上的其他控件添加了一个属性。这是通过extender provider 完成的。

【讨论】:

    【解决方案2】:

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

    添加了一个新的类文件: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 下的“属性”窗口中访问 Placeholder 属性。

    【讨论】:

      猜你喜欢
      • 2020-05-27
      • 2015-12-15
      • 2011-07-29
      • 1970-01-01
      • 2014-01-07
      • 2014-03-08
      • 2023-04-08
      • 2015-12-15
      • 1970-01-01
      相关资源
      最近更新 更多