Winforms 中的自定义控件库

发布于 2024-12-05 20:39:36 字数 895 浏览 0 评论 0原文

我想要一个仅包含数字作为条目的文本框。所以我在文本框的 KeyPress 事件中编写了以下代码

if(!char.IsDigit(e.KeyChar))
{
    e.handled = true;
}

,效果很好。但我通常在应用程序的许多地方都需要它们,所以我用以下代码编写了一个部分类:

public partial class digitTextBox : TextBox
{
    public digitTextBox()
    {
        this.KeyPress += new KeyPressEventHandler(digitTextBox_KeyPress);
    }

    void digitTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (char.IsDigit(e.KeyChar))
        {
            e.Handled = true;
        }
    }
}

构建我的解决方案,我在我的工具箱中得到了一个控件,即使这样也工作得很好。

但是我有很多文本框,它们具有某些或其他规范,例如,有些不应该接受特殊字符,有些应该接受小数,有些小数点后最多 2 位数字......等等,我确实需要这些类型的控件许多应用程序。

所以我想写一个我的自定义控件的库(.dll),如果可能的话甚至可以进行验证。老实说,我对使用库不太了解。因此,我创建了一个包含 2 种不同类型文本框的库,并用它们创建了一个 .dll 文件。现在,我创建了一个不同的 winform 应用程序,并添加了自定义控件 .dll 文件的引用。但什么也没发生。 所以我只是想知道我应该采取什么方法来实现它。有没有更好的方法来完成此类任务。也欢迎任何新的建议。提前致谢。

I wanted a TextBox with just digits as entry. So i wrote following code in the KeyPress events of the textbox

if(!char.IsDigit(e.KeyChar))
{
    e.handled = true;
}

and it worked great. But I generally need them in many places of my application so then i wrote a partial class with following codes:

public partial class digitTextBox : TextBox
{
    public digitTextBox()
    {
        this.KeyPress += new KeyPressEventHandler(digitTextBox_KeyPress);
    }

    void digitTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (char.IsDigit(e.KeyChar))
        {
            e.Handled = true;
        }
    }
}

build my solution and I got a control in my toolbox and even this was working perfectly fine.

But i had many TextBoxes with some or the other specification like, some should not accept special characters, some should accept decimals, some with decimals up to 2 digits after point....and so on and i do need these kind of controls in many applications.

So I thought of writing a Library(.dll) of my custom controls and if possible even for there validations. Honestly speaking I don't have much idea about using libraries. So I made a library with 2 different kind of textboxes and created a .dll file out of them. Now I created a different winform application and I added reference of my custom control .dll file. But nothing happened.
So i just wanted to know what should be my approach in achieving it. Is there a better way to achieve these kind of tasks. and any new suggestions are also welcome. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的他你的她 2024-12-12 20:39:36

尝试用鼠标右键单击工具箱并选择“选择项目...”,然后从可用列表中选择您的控件。如果您没有看到它们,请单击“浏览”按钮并选择您的 DLL。

顺便说一句,您也许可以通过添加属性来组合两个文本框:

public class MyTextBox : TextBox
{
  public bool AllowDigitsOnly { get; set; }

  protected override void OnKeyPress(KeyPressEventArgs e)
  {
    if (this.AllowDigitsOnly)
    {
      if (!char.IsDigit(e.KeyChar))
        e.Handled = true;
    }
    base.OnKeyPress(e);
  }
}

Try right-mouse clicking the ToolBox and select "Choose Items..." and then select your controls from the available list. If you don't see them, then click on the Browse button and select your DLL.

On a side note, you might be able to combine your two textboxes by adding properties:

public class MyTextBox : TextBox
{
  public bool AllowDigitsOnly { get; set; }

  protected override void OnKeyPress(KeyPressEventArgs e)
  {
    if (this.AllowDigitsOnly)
    {
      if (!char.IsDigit(e.KeyChar))
        e.Handled = true;
    }
    base.OnKeyPress(e);
  }
}
2024-12-12 20:39:36

我建议实现一个自定义控件,它根据正则表达式控制输入。如果控件的文本与正则表达式不匹配,我会防止控件失去焦点,而不是限制按键次数。背景颜色或弹出气球的一些变化可用于显示错误和/或输入内容的描述。

I would suggest implementing a single custom control, which controls input based on a regular expression. Instead of limiting keypresses, I would prevent the control loosing focus if its text does not match the regular expression. Some change in background color or a popup ballon can be used to display errors and/or a description of what the input is supposed to be.

贪恋 2024-12-12 20:39:36

我做了类似的事情,对我有帮助的是向我的文本框添加属性,而不是为整数和双精度创建两种不同的类型(基本上允许使用小数来满足我的要求)。

通过创建属性,您实际上可以在设计时在“属性”视图中选择文本框是整数还是双精度。就像您在设计时设置的字体、只读等一样。

这是我创建属性的代码,

        /// <summary>
        /// Identify textbox type as integer or double
        /// </summary>
        public enum numericID
        {
            Integer,
            Double
        };

        /// <summary>
        /// Textbox type property, default is integer
        /// </summary>
        private numericID numericType = numericID.Integer;

        /// <summary>
        /// Getter and setter for property
        /// </summary>
        [Browsable(true),
        DisplayName("TextBoxType"),
        DefaultValue(numericID.Integer),
        Description("Indicates whether the textbox must only accept integers or doubles."),
        Category("Behavior")]
        public numericID NumericType
        {
            get { return numericType; }
            set { numericType = value; }
        }

onKeyPress 事件实际上非常方便,来自 msdn 网站。它可以处理所有不同类型的数字字符,您可以选择最适合您的一种。

I did something similar and what helped me was to add properties to my textbox instead of creating 2 different types for integers and doubles (basically allowing a decimal for my requirements).

By creating a property, you can actually choose whether you want the textbox as integer or double in the Properties view at design-time. Just like how you have Font, ReadOnly etc that you set during design time.

Here is my code to make the properties,

        /// <summary>
        /// Identify textbox type as integer or double
        /// </summary>
        public enum numericID
        {
            Integer,
            Double
        };

        /// <summary>
        /// Textbox type property, default is integer
        /// </summary>
        private numericID numericType = numericID.Integer;

        /// <summary>
        /// Getter and setter for property
        /// </summary>
        [Browsable(true),
        DisplayName("TextBoxType"),
        DefaultValue(numericID.Integer),
        Description("Indicates whether the textbox must only accept integers or doubles."),
        Category("Behavior")]
        public numericID NumericType
        {
            get { return numericType; }
            set { numericType = value; }
        }

and the onKeyPress event is actually quite handy from the msdn website. It handles all different types of characters for numerics and you can choose whichever one works best for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文