C#中如何限制TextBox控件內(nèi)輸入值的范圍
更新時間:2023年01月24日 13:52:13 作者:Danny_hi
這篇文章主要介紹了C#中如何限制TextBox控件內(nèi)輸入值的范圍,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
C#限制TextBox控件內(nèi)輸入值的范圍
舉個例子
比如要限制TextBox1控件內(nèi)只能輸入1~100的數(shù)字(先將TextBox1的MaxLength屬性設(shè)置成3):
1.首先要限制輸入的只能是數(shù)值,不能是字母或其他符號;選擇添加textBox1的KeyPress事件
代碼如下:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
? ? ? ? {
? ? ? ? ? ? if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
? ? ? ? ? ? ? ? e.Handled = true;
? ? ? ? }2.再限制輸入數(shù)值的范圍1~100;選擇添加textBox1的TextChanged事件
代碼如下:
private void textBox1_TextChanged(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (textBox1.Text == "")?
? ? ? ? ? ? ?? ?textBox1.Text = 0.ToString();?
? ? ? ? ? ? int number = int.Parse(textBox1.Text);
? ? ? ? ? ? textBox1.Text = number.ToString();
? ? ? ? ? ? if (number <= 100)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? textBox1.Text = textBox1.Text.Remove(2);
? ? ? ? ? ? textBox1.SelectionStart = textBox1.Text.Length;
? ? ? ? }C#TextBox控件限制只允許輸入數(shù)字及小數(shù)點
//判斷按鍵是不是要輸入的類型。
?
? ? ? ? ? ? if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
?
? ? ? ? ? ? ? ? e.Handled = true;
?
?
? ? ? ? ? ? //小數(shù)點的處理。
?
? ? ? ? ? ? if ((int)e.KeyChar == 46) ? ? ? ? ? ? ? ? ? ? ? ? ? //小數(shù)點
?
? ? ? ? ? ? {
?
? ? ? ? ? ? ? ? if (textBox1.Text.Length <= 0)
?
? ? ? ? ? ? ? ? ? ? e.Handled = true; ? //小數(shù)點不能在第一位
?
? ? ? ? ? ? ? ? else
?
? ? ? ? ? ? ? ? {
?
? ? ? ? ? ? ? ? ? ? float f;
?
? ? ? ? ? ? ? ? ? ? float oldf;
?
? ? ? ? ? ? ? ? ? ? bool b1 = false, b2 = false;
?
? ? ? ? ? ? ? ? ? ? b1 = float.TryParse(textBox1.Text, out oldf);
?
? ? ? ? ? ? ? ? ? ? b2 = float.TryParse(textBox1.Text + e.KeyChar.ToString(), out f);
?
? ? ? ? ? ? ? ? ? ? if (b2 == false)
?
? ? ? ? ? ? ? ? ? ? {
?
? ? ? ? ? ? ? ? ? ? ? ? if (b1 == true)
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? e.Handled = true;
?
? ? ? ? ? ? ? ? ? ? ? ? else
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? e.Handled = false;
?
? ? ? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? }處理只輸入數(shù)字的
方法一:
private void tBox_KeyPress(object sender, KeyPressEventArgs e)?
? ?
?{?
? ? ? ? ? ? if (e.KeyChar == 0x20) e.KeyChar = (char)0; ?//禁止空格鍵?
? ? ? ? ? ? if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return; ? //處理負數(shù)?
? ? ? ? ? ? if (e.KeyChar > 0x20)?
? ? ? ? ? ? {?
? ? ? ? ? ? ? ? try?
? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? catch?
? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? e.KeyChar = (char)0; ? //處理非法字符?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? }?
}?方法二:
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)?
?{?
? ? if(e.KeyChar!=8&&!Char.IsDigit(e.KeyChar))?
? ? {?
? ? ? e.Handled = true;?
? ? }?
}?或者
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)?
{?
? ? if(e.KeyChar!='\b'&&!Char.IsDigit(e.KeyChar))?
? ? {?
? ? ? e.Handled = true;?
? ? }?
? ?
}?方法三:
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)?
{?
if(e.KeyChar!='\b')//這是允許輸入退格鍵?
{?
if((e.KeyChar<'0')||(e.KeyChar>'9'))//這是允許輸入0-9數(shù)字?
{?
e.Handled = true;?
}?
}?
}?方法四:
private void textBox1_Validating(object sender, CancelEventArgs e) ?
{ ?
const string pattern = @"^\d+\.?\d+{1}quot;; ?
string content = ((TextBox)sender).Text; ?
? ?
if (!(Regex.IsMatch(content, pattern))) ?
{ ?
errorProvider1.SetError((Control)sender, "只能輸入數(shù)字!"); ?
e.Cancel = true; ?
} ?
else ?
errorProvider1.SetError((Control)sender, null); ?
}?方法五:
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)?
{?
if(e.KeyChar=='.' && this.textBox1.Text.IndexOf(".")!=-1)?
{?
e.Handled=true;?
}?
? ?
if(!((e.KeyChar>=48 && e.KeyChar<=57) || e.KeyChar=='.' || e.KeyChar==8))?
{?
e.Handled=true;?
}?
? ?
}?方法六:
private void tbx_LsRegCapital_KeyPress(object sender, KeyPressEventArgs e)?
{?
? ? ? ? ? ? if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar))?
? ? ? ? ? ? {?
? ? ? ? ? ? ? ? e.Handled = true;//消除不合適字符?
? ? ? ? ? ? }?
? ? ? ? ? ? else if (Char.IsPunctuation(e.KeyChar))?
? ? ? ? ? ? {?
? ? ? ? ? ? ? ? if (e.KeyChar != '.' || this.textBox1.Text.Length == 0)//小數(shù)點?
? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? e.Handled = true;?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? if (textBox1.Text.LastIndexOf('.') != -1)?
? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? e.Handled = true;?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? } ? ? ??
? } ??方法七:
利用ASCII碼處理辦法、
{?
? ?
? ? ? ? ? ? if ((e.KeyChar <= 48 || e.KeyChar >=57) && (e.KeyChar != 8) && (e.KeyChar != 46))?
? ? ? ? ? ? ? e.Handled = true;?
================48代表0,57代表9,8代表空格,46代表小數(shù)點?
}C# 文本框只能輸入數(shù)字和退格鍵
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
? {
? ?if(e.KeyChar!=8&&!Char.IsDigit(e.KeyChar))
? ?{
? ? ?e.Handled = true;
? ?}
? }或者
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
? {
? ?if(e.KeyChar!='\b'&&!Char.IsDigit(e.KeyChar))
? ?{
? ? ?e.Handled = true;
? ?}
? }判斷是否為空
?if (string.IsNullOrWhiteSpace(txtDir.Text))//指示指定的字符串是 null、空還是僅由空白字符組成。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用C#實現(xiàn)Windows組和用戶管理的示例代碼
這篇文章主要介紹了使用C#實現(xiàn)Windows組和用戶管理的示例代碼,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-01-01

