【问题标题】:Validate textbox on key down event to accept only numbers in silverlight 4验证按键事件的文本框以仅接受 silverlight 4 中的数字
【发布时间】:2014-04-08 17:38:21
【问题描述】:

我需要验证来检查按下的键是否为数字。我尝试了不同的代码,但他们无法帮助我。在文本框中,如果用户按Shift+numbers,它会显示特殊字符,如!,@,#......我需要验证Shift + key down event

//代码

    private void txtNumericTextbox_KeyDown(object sender, KeyEventArgs e)
            {
                try
                {
                    if (e.Key < Key.D0 || e.Key > Key.D9)
                    {
                        if (e.Key < Key.NumPad0 || e.Key > Key.NumPad9)
                        {
                            if (e.Key != Key.Back)
                            {
                                txtNumericTextbox_.BorderBrush = new SolidColorBrush(Colors.Red);
                                lblErrorMessage.Visibility = Visibility.Visible;
                                lblErrorMessage.Text = "Please Enter Numbers Only";
                            }
                            else
                            {
                                txtNumericTextbox_.BorderBrush = new SolidColorBrush(Colors.DarkGray);
                                lblErrorMessage.Visibility = Visibility.Collapsed;
                                lblErrorMessage.Text = "";
                            }
                        }
                    }
                 }
             }

我怎样才能做到这一点?

【问题讨论】:

  • 通常当我处理数字输入时,我只是简单地在TextChanged 事件上尝试Int32.TryParseDouble.TryParse。它使处理负号、小数或其他错误按键进行编辑(删除、复制/粘贴、箭头左/右键)的情况变得简单,然后只显示“请插入数字”的一般错误。
  • 如果您在属性中使用ValidationException 绑定到您的Textbox.Text 属性,则文本框会自动设置红色边框和消息。见Silverlight validation

标签: c# silverlight textbox


【解决方案1】:

您可以使用控件上的 ModifierKeys 属性来确定是否按住 shift 键。

//代码

使用它只接受数值。 事件可以选择 textBox1_KeyDown nonnumberenter = false;

            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    if (e.KeyCode != Keys.Back)
                    {
                        nonnumberenter = true;
                        string abc = "Please enter numbers only.";
                        DialogResult result1 = MessageBox.Show(abc.ToString(), "Validate numbers", MessageBoxButtons.OK);
                    }
                }
            }
            if (Control.ModifierKeys == Keys.Shift)
            {
                nonnumberenter = true;
                string abc = "Please enter numbers only.";
                DialogResult result1 = MessageBox.Show(abc.ToString(), "Validate numbers", MessageBoxButtons.OK);

            }

使用它只接受字符。事件你可以选择 textBox1_KeyPress

if (Char.IsNumber(e.KeyChar) || Char.IsSymbol(e.KeyChar) || Char.IsWhiteSpace(e.KeyChar) || Char.IsPunctuation(e.KeyChar))
            {
                MessageBox.Show("Only Char are allowed");
                e.Handled = true;
            }

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    您可以使用文本框按键事件

    private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) )
            {
                e.Handled = true;
            }
        }
    

    【讨论】:

      【解决方案3】:

      我也在寻找同样的东西:只接受数字。但是,我在我的_KeyDown 活动中没有找到任何e.KeyCode,所以我调整了 Kumar 的代码以满足我的需要,并与你分享,如果它更适合你: 使用e.Handled = true 取消该字符的输入。

          private void textBox1_KeyDown(object sender, KeyEventArgs e)
          {
              switch (e.Key)
              {
                  case Key.NumPad0:
                  case Key.NumPad1:
                  case Key.NumPad2:
                  case Key.NumPad3:
                  case Key.NumPad4:
                  case Key.NumPad5:
                  case Key.NumPad6:
                  case Key.NumPad7:
                  case Key.NumPad8:
                  case Key.NumPad9:
                  case Key.D0:
                  case Key.D1:
                  case Key.D2:
                  case Key.D3:
                  case Key.D4:
                  case Key.D5:
                  case Key.D6:
                  case Key.D7:
                  case Key.D8:
                  case Key.D9:
                      break;
                  default:
                      e.Handled = true;
                      break;
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-06
        • 2011-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-24
        相关资源
        最近更新 更多