课本中的字符输入限制 [英] character entry restriction in the text book

查看:98
本文介绍了课本中的字符输入限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文本框中,应该只允许用户输入6个小数位.例如:1.012345或1,012345
如果尝试使用小数点后7位,则不允许该条目.
请参考下面的代码

On the text box, it should allow the user to enter only 6 decimal places. Ex: 1.012345 or 1,012345
If 7 decimal places are tried, the entry should not be allowed.
Please refer the below code

private void textBox1_KeyDown_1(object sender, KeyEventArgs e)
       {
           /// <summary>


           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;
               //convert it into the culture invariant type

               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;
           }
       }
   }



拆分字符串以分离实数部分和尾数部分(两者都存储为字符串),然后检查尾数部分的长度.如果字符串的长度大于5,我想隐藏该键.



Splitting the strings to seperate the real part and mantissa part (both are stored are strings), then I check the length of the mantissa part. If the length of the string is more than 5, I''d like to suppress the key.

Is there any other approach to do this?

推荐答案

>还有其他方法可以做到这一点吗?
是的.使用MaskedTextBoxNumericUpDown.
可以轻松设置它们两者以匹配预期的行为.
我相信,只需分别使用属性MaskDecimals.
> Is there any other approach to do this?
Yes. Use a MaskedTextBox or a NumericUpDown.
Both of them can be easily set up to match the expected behaviour.
Just use the properties Mask, respectively Decimals, i believe.


您可以按如下方式使用RegEx并根据需要对其进行修改.您可以轻松地将其转换为C# [
You can use RegEx as follows and modify it as per your need. You can easily convert it to C#[^]
If Not isValid(TextBox1.Text) Then
  'block action here
End If

Function isValid(ByVal value As String) As Boolean
  Dim rex As New Regex("^\d+((\.|,)\d{0,6})?


") 返回 rex.Match(值).成功 结束 功能
") Return rex.Match(value).Success End Function







if (!isValid(TextBox1.Text)) {
        //block action here
}

public bool isValid(string value)
{
    Regex rex = new Regex("^\d+((\.|,)\d{0,6})?


这篇关于课本中的字符输入限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆