如何设置文本框仅显示数字,最大值为11

编程入门 行业动态 更新时间:2024-10-10 01:27:15
本文介绍了如何设置文本框仅显示数字,最大值为11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何设置文本框仅显示数字,最大值为11

how do i set text box to display numbers only and the maximum value is 11

推荐答案

使用一个子程序处理几个文本框的Keypress事件。 /> Use one subroutine to handle the Keypress event for several textboxes. Private Sub SeveralTextboxes_KeyPress(sender As Object, e As KeyPressEventArgs) _ Handles textbox1.KeyPress,textbox2,KeyPress,textbox3.KeyPress, textbox4.KeyPress If e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "-"C Then 'allow backspace for deleting and minus simbol e.Handled = Not Char.IsNumber(e.KeyChar) 'allow numbers only If Not e.Handled Then Dim num As Integer = _ Integer.Parse(String.Format("{0}{1}", _ If(DirectCast(sender,TextBox).Text = String.Empty, _ "", DirectCast(sender,TextBox).Text), e.KeyChar.ToString())) If num > 11 Then e.Handled = True End If End If End If End Sub

你可以从这里开始: Windows窗体中的用户输入验证 [ ^ ] You may start here: User Input Validation in Windows Forms[^]

我尝试了这个简单的代码,但是如何为表格中的所有文本框设置这个代码 I tried this simple code but how to set this for all textboxes in the form Private Sub textBox1_KeyPress(sender As Object, e As KeyPressEventArgs) If e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "-"C Then 'allow backspace for deleting and minus simbol e.Handled = Not Char.IsNumber(e.KeyChar) 'allow numbers only If Not e.Handled Then Dim num As Integer = Integer.Parse(String.Format("{0}{1}", If(textBox1.Text = String.Empty, "", textBox1.Text), e.KeyChar.ToString())) If num > 11 Then e.Handled = True End If End If End If End Sub

更多推荐

如何设置文本框仅显示数字,最大值为11

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

发布评论

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

>www.elefans.com

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