如何设置文本框仅显示数字,最大值为11 [英] How Do I Set Text Box To Display Numbers Only And The Maximum Value Is 11
本文介绍了如何设置文本框仅显示数字,最大值为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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文