【问题标题】:"include" a class in a windows form C#在 Windows 窗体 C# 中“包含”一个类
【发布时间】:2021-09-13 09:45:34
【问题描述】:

我有一个包含几个文本框的表单。加载表单时,我希望这些文本框显示默认的灰色文本,当用户单击文本框时该文本消失。 我用过这段代码:

    private void Form1_Load(object sender, EventArgs e)
    {
        //textbox1 default text
        this.tBox1.Enter += new EventHandler(tBox1_Enter);
        this.textbox1.Leave += new EventHandler(tBox1_Leave);
        tBox1_SetText();
        
        /*...
         same for other textboxes
        */

    }

    /*textbox1 default text*/

    // default greyed out text in tBox1
    public void tBox1_SetText()
    {
        tBox1.Text = "some default text";
        tBox1.ForeColor = Color.Gray;
    }

    // remove default text from tBox1 once cruser is set in
    public void tBox1_Enter(object sender, EventArgs e)
    {
        if (tBox1.ForeColor == Color.Black)
            return;
        tBox1.Text = "";
        tBox1.ForeColor = Color.Black;
    }

    // sets the default text back to tBox1 once cruser is gone
    public void tBox1_Leave(object sender, EventArgs e)
    {
        if (tBox1.Text.Trim() == "")
            tBox1_SetText();
    }

    /*...
     same for other textboxes
    */

因为我有很多文本框,我想将所有这些代码从我的表单移动到一个单独的类(类似于 DefaultText.cs),在该类中创建一个方法(“ShowDefaultText()”)并调用它form_load 中的方法,所以我的代码会更容易理解。 我尝试创建此类,但 C# 中没有#include,并且由于实例,我不能使用静态类/方法。 否则我该怎么做?

【问题讨论】:

  • 这就是#using-directive 的用途
  • 你正在处理这个问题 - 创建一个组件来包装你的“占位符”文本并将其放置在你需要的默认文本框的位置
  • @Jamiec 这正是我试图避免的。我不想把它放在我需要的地方,因为我不想在我的 form.cs 中有 100 行,专门用于在文本框中设置一些文本。我想要不同类中的所有功能,并且只在我的表单代码中编写一个简单的行。
  • 你错过了我的观点——通过将它作为一个组件,你有 no 行代码重复。使该文本变为灰色和占位符的代码仅存在一次 - 在组件中。
  • @Jamiec 是的,但不幸的是我需要每个文本框来显示不同的文本,所以我必须为每个文本框手动设置它,我想避免所有这些代码行,因为我的代码已经很长而且这些行并不重要。

标签: c# winforms class


【解决方案1】:

正如 Jamiec 指出的那样,如果您使用的是 .net Core 3.1 或更高版本,那么您可以使用 TextBox.PlaceholderText 属性来实现您的目标。

但是,如果您使用没有该属性的 .net framework 4.8(或更早版本),最好的方法是编写一个自定义的 TextBox 类来为您执行此操作 - 那么您只需要使用它而不是 TextBox

例如:

首先,使用Add | New Item 将自定义控件添加到您的项目中,然后选择“Windows 窗体”并选择“自定义控件”并单击“添加”。将其命名为 TextBoxGreyed

然后将实现编辑为如下所示:

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

namespace WinFormsApp1
{
    public partial class TextBoxGreyed : TextBox
    {
        public TextBoxGreyed()
        {
            InitializeComponent();
            setText();
        }

        [
            Browsable(true),
            Category("Appearance"),
            DefaultValue(""),
            Localizable(true),
            Description("Default text to display when text box is empty")
        ]

        public string DefaultText
        {
            get => _defaultText;

            set
            {
                _defaultText = value;
                setText();
                Invalidate();
            }
        }

        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);

            if (this.ForeColor == Color.Black)
                return;

            this.Text      = "";
            this.ForeColor = Color.Black;
        }

        protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);

            if (this.Text.Trim() == "")
                setText();
        }

        void setText()
        {
            this.Text      = _defaultText;
            this.ForeColor = Color.Gray;
        }

        string _defaultText = "";
    }
}

构建后,您应该会在表单设计器的工具箱顶部看到新的自定义控件。如果您将 TextBoxGreyed 添加到表单并检查其属性,您应该会看到一个 DefaultText 属性,您可以将其设置为所需的文本。

这是一个相当基本的示例,但希望它能帮助您入门。

【讨论】:

  • OP 这是我在我的 cmets 中描述的解决方案 - 先到那里,所以我不需要发布相同的答案。
  • 这个解决方案的唯一问题是,通过在构造函数中调用setText(在设置DefaultText之前)它会在第一次加载时为空,直到失去焦点
  • @Jamiec 我会在几分钟内解决这个问题。敬请期待;)
  • 认为我们都在打死马:PlaceholderText
  • @Jamiec 我修复了初始化。至于PlaceholderText - 这在.net 4.8 中不可用,因此OP 可能无法使用它。如果是这样,这仍然有用!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-04
  • 1970-01-01
  • 1970-01-01
  • 2011-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多