【问题标题】:Disable spacekey textbox in c# wpf在 c# wpf 中禁用空格键文本框
【发布时间】:2015-10-07 06:53:51
【问题描述】:

我有只允许数字、删除和退格的文本框。此代码适用于此,但即使我禁用它,它也允许空格键。我不确定这里缺少什么。

public class NumberTextBox : TextBox
{
    /// <summary>
    /// Allows only numbers, delete, backspace keys
    /// </summary>
    public NumberTextBox()
    {
        KeyDown += new KeyEventHandler(OnKeyDown);
    }

    private bool IsNumberKey(Key inKey)
    {
        if (inKey < Key.D0 || inKey > Key.D9)
        {
            if (inKey < Key.NumPad0 || inKey > Key.NumPad9)
            {
                return false;
            }
        }
        return true;
    }

    private bool IsActionKey(Key inKey)
    {
        return inKey == Key.Delete || inKey == Key.Back;
    }

    private bool IsSpaceKey(Key inKey)
    {
        if (inKey == Key.Space)
        {
            return true;
        }
        return false;
    }

    protected void OnKeyDown(object sender, KeyEventArgs e)
    {
        e.Handled = !IsNumberKey(e.Key) && !IsActionKey(e.Key) && IsSpaceKey(e.Key);
    }

}

【问题讨论】:

  • 这应该对你有帮助:stackoverflow.com/questions/16125470/… 只要确保输入键是数字,否则将其抑制。
  • 要修复此代码,您应该使用:e.Handled = (!IsNumberKey(e.Key) &amp;&amp; !IsActionKey(e.Key)) || IsSpaceKey(e.Key);
  • @SepehrM,感谢您的评论。我添加了您的建议,但它不起作用,但我将其更改为 PreviewKeyDown 而不是 KeyDown,所以它现在可以工作了。 PreviewKeyDown += new KeyEventHandler(OnKeyDown);

标签: c# wpf textbox space


【解决方案1】:

来自microsoft website:

public class NumericTextBox : TextBox
{
    bool allowSpace = false;

    // Restricts the entry of characters to digits (including hex), the negative sign,
    // the decimal point, and editing keystrokes (backspace).
    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        base.OnKeyPress(e);

        NumberFormatInfo numberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
        string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
        string groupSeparator = numberFormatInfo.NumberGroupSeparator;
        string negativeSign = numberFormatInfo.NegativeSign;

        string keyInput = e.KeyChar.ToString();

        if (Char.IsDigit(e.KeyChar))
        {
            // Digits are OK
        }
        else if (keyInput.Equals(decimalSeparator) || keyInput.Equals(groupSeparator) ||
         keyInput.Equals(negativeSign))
        {
            // Decimal separator is OK
        }
        else if (e.KeyChar == '\b')
        {
            // Backspace key is OK
        }
        //    else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0)
        //    {
        //     // Let the edit control handle control and alt key combinations
        //    }
        else if (this.allowSpace && e.KeyChar == ' ')
        {

        }
        else
        {
            // Swallow this invalid key and beep
            e.Handled = true;
            //    MessageBeep();
        }
    }

    public int IntValue
    {
        get
        {
            return Int32.Parse(this.Text);
        }
    }

    public decimal DecimalValue
    {
        get
        {
            return Decimal.Parse(this.Text);
        }
    }

    public bool AllowSpace
    {
        set
        {
            this.allowSpace = value;
        }

        get
        {
            return this.allowSpace;
        }
    }
}

【讨论】:

  • 对于winforms,我很抱歉,wpf 需要它。
  • 概念是一样的,只要正确处理e.Handled@Santosh
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-06
  • 1970-01-01
  • 1970-01-01
  • 2013-12-05
  • 1970-01-01
相关资源
最近更新 更多