限制文本框中的字符

时间:2011-04-13 09:57:44

标签: c# winforms

在文本框中,应该允许用户只输入六位小数。例如,1.012345或1,012345。

如果尝试七位小数,则不应允许输入。

请参阅以下代码。

private void textBox1_KeyDown_1(object sender, KeyEventArgs e)
{
    bool numericKeys = (
        ( ((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) ||
           (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)) &&
              e.Modifiers != Keys.Shift)  ||
              e.KeyCode == Keys.OemMinus  ||
              e.KeyCode == Keys.OemPeriod ||
              e.KeyCode == Keys.Decimal   ||
              e.KeyCode == Keys.Oemcomma
        );

    bool navigationKeys = (
        e.KeyCode == Keys.Up ||
        e.KeyCode == Keys.Right ||
        e.KeyCode == Keys.Down ||
        e.KeyCode == Keys.Left ||
        e.KeyCode == Keys.Home ||
        e.KeyCode == Keys.End ||
        e.KeyCode == Keys.Tab);

    bool editKeys = (e.KeyCode == Keys.Delete ||
                     e.KeyCode == Keys.Back);

    TextBox aTextbox = (TextBox)sender;
    aTextbox.Text = "2,33333";

    if (!string.IsNullOrEmpty(aTextbox.Text))
    {
        double maxIntensity;
        string aTextValue = aTextbox.Text;
        bool value = double.TryParse(aTextValue,
                                     NumberStyles.Any,
                                     CultureInfo.InvariantCulture.NumberFormat,
                                     out maxIntensity);

        if (value)
        {
            string aText = maxIntensity.ToString();
            MessageBox.Show("the value is {0}", aText);
            string[] args = aText.Split('.');

            if (numericKeys)
            {
                bool handleText = true;

                if (args.Length > 2)
                {
                    handleText = false;
                }
                else
                {
                    if (args.Length == 2 && args[1] != null && args[1].Length > 5)
                    {
                        handleText = false;
                    }
                }

                if (!handleText)
                {
                    e.SuppressKeyPress = true;
                    e.Handled = true;
                }
            }
        }
    }

    if (!(numericKeys || editKeys || navigationKeys))
    {
        e.SuppressKeyPress = true;
        e.Handled = true;
    }
}

为了实现文化中立,首先将文本值转换为double然后再转换为double string值转换为bool value = double.TryParse(aTextValue, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out maxIntensity); if (value) { string aText = maxIntensity.ToString(); }

{{1}}

分割字符串以分隔实部和尾数部分(两者都存储为字符串),然后检查尾数部分的长度。如果字符串的长度大于5,我想要取消密钥。

还有另一种方法吗?

2 个答案:

答案 0 :(得分:1)

我很幸运遇到类似以下的代码:

public class RegexMaskedTextBox : TextBox
{
    private Regex _regex = new Regex(string.Empty);

    public string RegexPattern
    {
        get { return _regex.ToString(); }
        set { _regex = new Regex(value); }
    }

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        string sNewText = this.Text;
        string sTextToInsert = e.KeyChar.ToString();
        if (this.SelectionLength > 0)
        {
            sNewText = this.Text.Remove(this.SelectionStart, this.SelectionLength);
        }
        sNewText = sNewText.Insert(this.SelectionStart, sTextToInsert);
        if (sNewText.Length > this.MaxLength)
        {
            sNewText = sNewText.Substring(0, this.MaxLength);
        }
        e.Handled = !_regex.IsMatch(sNewText);
        base.OnKeyPress(e);
    }
}

答案 1 :(得分:0)

我想我会用正则表达式来解决这个问题。在按键事件中,您可以检查正则表达式。

也许您想查看一下:Regular expression for decimal number