如何允许在C#Windows窗体中只允许12个数字输入文本框 [英] How Can I Allow To Enter Only 12 Numbers Into Textbox In C# Windows Form

查看:226
本文介绍了如何允许在C#Windows窗体中只允许12个数字输入文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述








如何在c#windows form

Hi


How can i allow to enter only 12 numbers into textbox in c# windows form

推荐答案

最简单的答案是将MaxLength属性设置为12.



有很多方法可以解决这个简单的设置复制粘贴等。如果您需要MaxLength,那么只需接受解决方案即可。如果您还需要处理其他问题,请在下面发表评论,我将添加到此解决方案。



希望有所帮助^ _ ^

Andy



更新:我从评论中了解到OP需要输入最多12个数字(对于Maciej Los ^ _ ^):



如果你想让这个字段只是数字,那么也有办法做到这一点:



The simplest answer is to set the MaxLength property to 12.

There are ways around that simple setting with copy-paste etc. If the MaxLength is all you need then just accept the solution. If you need to deal with the other issues too, then comment below and I'll add to this solution.

Hope that helps ^_^
Andy

UPDATE: I have learnt from comments that the OP requires max of 12 numbers to be entered (thx Maciej Los ^_^):

If you want the field to be numeric only then there are ways to do this too:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    //We only want to allow numeric style chars
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
        (e.KeyChar != '.'))
    {
            //Setting e.Handled cancels the keypress event, so the key is not entered
            e.Handled = true;
    }

    // we only allow one decimal point here
    if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
    {
        e.Handled = true;
    }
    //keypress occurs before the text is updated.  Therefore sender.Text excludes the current key e.KeyChar 
}







将它与MaxLength结合使用,你将得到一个12字符长的数字字段。





如果你想要一个x字节的文本字段可能包含最多12位数的字符,然后你可以再次使用按键事件,只是不要设置MaxLength:






Combine this with the MaxLength and you will have a numeric field 12 chars long.


If you want a textfield for x number of chars that may contain a max on 12 digits, then you can again use the keypress event, just don't set MaxLength:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    // same as testing for decimal above, we can check the text for digits
    if (char.IsDigit(e.KeyChar))
    {
        //Count the digits already in the text.  I'm using linq:
        if((sender as TextBox).Text.Count(Char.IsDigit)>=12)
            e.Handled = true;
    }
}





我希望这个解决方案更能满足您的需求^ _ ^





来自OP的更新评论:

OP正在使用Telerik套件中的RadMaskedEditBox。



RadMaskedEditBox有自己的处理程序来限制输入。这些被称为掩码(因此为radMASKEDeditbox ^ _ ^)



如果使用这些,则不需要自定义格式事件。 RadMaskedEditBox将为您完成。



您可以在控件属性窗口中找到属性,也可以在表单加载时添加它们。



MaskType =标准(来自下拉列表); //控件默认屏蔽

Mask =0000000000(以文本形式输入); //控件掩码模式这表示12位数



如果在属性窗口中找不到这些选项,请在表单加载中添加它们,如下所示:



myRadMaskedEditBox.MaskType = MaskType.Standard;

myRadMaskedEditBox.Mask =000000000000;



希望清除混淆^ _ ^



I hope this solution better suites your needs ^_^


UPDATE from OPs comments:
OP is using a RadMaskedEditBox from the Telerik suite.

The RadMaskedEditBox has it's own handlers for restricting input. These are called Masks (hence radMASKEDeditbox ^_^)

If you use these then you do not require custom format events. The RadMaskedEditBox will do it for you.

You can find the properties in the controls Properties window, or you can add them on form load.

MaskType = Standard (from dropdown); //The controls default masking
Mask = "0000000000" (enter as text); // The controls mask pattern. This represents 12 digits

If you can't find these options in the properties window then add them on form load like this:

myRadMaskedEditBox.MaskType = MaskType.Standard;
myRadMaskedEditBox.Mask = "000000000000";

Hope that clears up the confusion ^_^


我建​​议阅读MSDN文章:

MaxLength Property [ ^ ]

如何:创建数字文本框 [ ^ ]

如何:控制数字文本框中的用户输入 [ ^ ]
I'd suggest to read MSDN articles:
MaxLength Property[^]
How to: Create a Numeric Text Box[^]
How to: Control User Input in a Numeric Text Box[^]


处理TextChanged事件,并检查通过正则表达式:

Handle the TextChanged event, and check it via a regex:
\d{1,12}

应该这样做。

如果失败正则表达式,恢复以前的版本。如果它通过,将其保存为以前的版本。

恢复时,使用SelectionStart确保光标不移动,在设置文本之前读取光标,并将其设置回之后的价值。

should do.
If it fails the regex, restore the previous version. If it passes, save it as the previous version.
When you restore, use the SelectionStart to ensure that the cursor doesn't move, by reading it before you set the text, and setting it back to that value afterwards.


这篇关于如何允许在C#Windows窗体中只允许12个数字输入文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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