C# 帮助创建自定义控件

C# Help Creating Custom Control

我正在尝试制作一个自定义控件,它根据所选选项接受输入。我想将小数点限制为只有一个,这样用户就不会输入多个“。”我怎样才能做到这一点?

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

namespace DT_Controls
{
    public enum Options { Any, Alphabets, Alpha_Numeric, Numeric }

    public class TextBox_Pro : TextBox
    {
        Options _Opt = 0;
        bool _Flag = false;
        int Count = 0;

        [Category("Behavior")]
        [Description("If set as true will accept decimal values when SetOption is Numeric")]
        public bool AcceptDecimal
        {
            get { return _Flag; }
            set { _Flag = value; }
        }

        [Category("Behavior")]
        [Description("Controls the type of value being entered into the TextBox_Pro")]
        public Options SetOption
        {
            get { return _Opt; }
            set { _Opt = value; }
        }

        public TextBox_Pro()
        {
            this.KeyPress += TextBox_Pro_KeyPress;
        }

        private void TextBox_Pro_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt32(e.KeyChar) == 8)
                return;
            switch (_Opt)
            {
                case Options.Numeric:
                    if (_Flag == true)
                        if (Convert.ToInt32(e.KeyChar) == 46)
                            return;
                    if (char.IsDigit(e.KeyChar) == false)
                    {
                        MessageBox.Show("Enter Numeric Values Only");
                        e.Handled = true;
                    }
                    break;
                case Options.Alphabets:
                    if(char.IsLetter(e.KeyChar)==false && Convert.ToInt32(e.KeyChar) != 32)
                    {
                        MessageBox.Show("Enter Only Aplhabets");
                        e.Handled = true;
                    }
                    break;
                case Options.Alpha_Numeric:
                    if (char.IsLetterOrDigit(e.KeyChar) == false)
                    {
                        MessageBox.Show("Enter Only Alphabets Or Numbers");
                        e.Handled = true;
                    }
                    break;
            }
        }
    }
}

示例我不希望用户输入 12.....123 我希望用户输入 12.123 和 1 之后。它应该禁用该标志,但是当我这样做时它不允许我输入任何“。”即使删除了“。”

比设置任何标志更容易的是检查您的文本框是否已经包含任何“.”。与内置功能。因此,替换以下代码:

if (_Flag == true)
    if (Convert.ToInt32(e.KeyChar) == 46)
        return;

与:

if (Convert.ToInt32(e.KeyChar) == 46)
    {
        if (this.Text.Contains("."))
            {
                e.Handled = true;
            }
            return;
    }

这样您还可以检查其他不一致之处,例如检查 TextBox 中的文本是否以“.”开头。

不同的文化使用不同的数字小数点分隔符,因此最好从 CultureInfo.CurrentCulture.NumberFormat 中读取,而不是硬编码一个值。因为您是从 TextBox 继承的,所以您可以覆盖 OnKeyPress 而不是订阅 KeyPress 事件。如果小数分隔符是第一个字符,则以下示例还会自动显示 0。

[ToolboxBitmap(typeof(TextBox))]
public class TextBoxPro
    : TextBox
{
    static NumberFormatInfo       s_numberFormatInfo = CultureInfo.CurrentCulture.NumberFormat;

    public static readonly string s_decimalSeparator = s_numberFormatInfo.NumberDecimalSeparator;

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        base.OnKeyPress(e);

        if (ReadOnly)
            return;

        var keyChar   = e.KeyChar;
        var keyString = keyChar.ToString();

        if (keyString == s_decimalSeparator)
        {
            if (IsAllSelected)
            {
                e.Handled      = true;
                Text           = "0.";
                SelectionStart = 2;
            }
            else if (Text.Contains(s_decimalSeparator))
                e.Handled = true;
        }
    }

    private bool IsAllSelected
    {
        get { return SelectionLength == TextLength; }
    }
}