Textbox Max Min值 [英] Textbox Max Min value
本文介绍了Textbox Max Min值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
首先我要说这是一个有点愚蠢的问题。
有没有办法可以设置 max
(246)和 min
(0)文本框的值
?
我知道我可以做...
Let me start off by saying this is a kinda dumb question.
Is there a way you can set the max
(246) and min
(0) value of a textbox
?
I know I could do …
If DataSizeText.Text < 0 Then
DataSizeText.Text = 0
ElseIf DataSizeText.Text > 246 Then
IndexText.Text = 246
DataSizeText.Text = 246
End if
您还有什么方法可以设置最大值和最小值?比如可能使用属性工具栏?
What other way could you go about setting max and min value? Like maybe use the properties tool bar ?
推荐答案
没有。 Text属性是一个字符串,而不是整数,所以max和min对它没有任何意义。
相反,请考虑使用NumericUpDown代替。
No. The Text Property is a string, not an integer, so "max" and "min" don't mean anything to it.
Instead, consider using a NumericUpDown instead.
您没有使用WinForm
或ASP.NET
如果你想在中使用Windows Forms
应用程序
那么NumericUpDown
可以使用控件,可以设置最小
和最大
属性达到所需的价值。分别为0,246。
这里给出一个例子
http://msdn.microsoft.com/en-us/library/system.windows.forms.numericupdown.minimum.aspx [ ^ ]
如果你想在中使用ASP.NET
应用程序
那么TextBox
控制以及RangeValidator
控件可以使用。
RangeValidator
在这里解释
http://www.w3schools.com/aspnet/control_rangevalidator.asp [ ^ ]
它可以在线测试
http://www.w3schools.com/aspnet/showasp.asp?filename=demo_rangevalidator2 [ ^ ]
和
http://www.w3schools.com/aspnet/showasp.asp?filename=demo_rangevalidator [ ^ ]
You have not tagged your question either withWinForm
orASP.NET
If you want to use inWindows Forms
application
ThenNumericUpDown
control can be used andMinimum
andMaximum
properties can be set to the values required. i.e. 0, 246 respectively.
An example is given here
http://msdn.microsoft.com/en-us/library/system.windows.forms.numericupdown.minimum.aspx[^]
If you want to use inASP.NET
application
ThenTextBox
control along withRangeValidator
control can be used.
TheRangeValidator
is explained here
http://www.w3schools.com/aspnet/control_rangevalidator.asp[^]
It can be tested online at
http://www.w3schools.com/aspnet/showasp.asp?filename=demo_rangevalidator2[^]
and
http://www.w3schools.com/aspnet/showasp.asp?filename=demo_rangevalidator[^]
我试过这个简单的代码但是如何为表单中的所有文本框设置这个?
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
这篇关于Textbox Max Min值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文