如何创建仅接受某些整数值的文本框?

编程入门 行业动态 更新时间:2024-10-25 18:31:33
本文介绍了如何创建仅接受某些整数值的文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我在网上搜索过,但找不到我要找的东西.我正在使用 VS2010 并尝试制作一个 w7p 应用程序.我正在尝试制作一个仅接受 0 到 5 之间的单个整数值的文本框,因此没有负数也没有小数,因为任何更高的值都会使应用程序崩溃.谢谢!

I've searched the web and was unable to find what I am looking for. I'm using VS2010 and trying to make a w7p app. I'm trying to make a textbox that only accepts a single integer value from 0 to 5, so no negative and no decimal as any value higher crashes the app. Thank you!

推荐答案

使用 NumericUpDown 控件,如果有的话你可以只设置 Minimum>最大属性.

Use a NumericUpDown control, if there is one and you can just set the Minimum and Maximum properties.

如果没有,在文本框的 KeyDown 事件中使用它:

If there's not, use this in the KeyDown event of the textbox:

List<Keys> allowedKeys = new List<Keys>()
{
    Keys.Back, Keys.D0, Keys.D1, Keys.D2, Keys.D3, Keys.D4, Keys.D5,
    Keys.NumPad0, Keys.NumPad1, Keys.NumPad2, Keys.NumPad3, Keys.NumPad4, Keys.NumPad5
};

e.SuppressKeyPress = !allowedKeys.Contains(e.KeyCode);

这将抑制任何不是 0-5BACKSPACE 键的东西.此外,将您的 Maximum Length 属性设置为 1,它将允许 only 0 到 5.

This will suppress anything that is not a 0-5 or BACKSPACE key. Also, set your Maximum Length property to 1, it will allow only 0 through 5.

这篇关于如何创建仅接受某些整数值的文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-25 14:24:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117753.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   文本框

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!