本文介绍了如何设置文本框仅显示数字,最大值为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
发布评论