[英]How to make a textBox accept only Numbers and just one decimal point in Windows 8
[英]How Force textbox to just accept numbers between 2.1 to 9.5
在C#和Windows窗體應用程序中,
我有一個文本框,我想強制用戶只能輸入數字,而只能輸入一個特定的數字,例如,在這種情況下,我的數字范圍是{2.1、2.2,...,9.4、9.5}
這就是我強制文本框只接受數字的方式:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!((int)e.KeyChar>=48 && (int)e.KeyChar<= 57 || (int)e.KeyChar ==8))
{
e.KeyChar = (char)27;
}
}
但我不知道如何設置范圍:僅在{2.1到9.5}或{25到91}之間
對於WinForms,只需使用NumericUpDown即可 。
將其放在表單上,根據自己的喜好和完成的工作設置Minimum
, Maximum
, DecimalPlaces
和Increment
參數。
通過控件的.Value
屬性訪問該值。
為文本框添加一個regexvalidator控件,並在其中使用正則表達式以確保僅可以輸入所需的數字。 我不知道正則表達式,但是它應該很容易。
就像是 -
<asp:RegularExpressionValidator ValidationExpression="Expression Goes Here" ControlToValidate="YourControl" ID="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator"></asp:RegularExpressionValidator>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.