C#中如何限制TextBox控件內(nèi)輸入值的范圍
C#限制TextBox控件內(nèi)輸入值的范圍
舉個(gè)例子
比如要限制TextBox1控件內(nèi)只能輸入1~100的數(shù)字(先將TextBox1的MaxLength屬性設(shè)置成3):
1.首先要限制輸入的只能是數(shù)值,不能是字母或其他符號(hào);選擇添加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ù)點(diǎn)
//判斷按鍵是不是要輸入的類型。 ? ? ? ? ? ? ? if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46) ? ? ? ? ? ? ? ? ? e.Handled = true; ? ? ? ? ? ? ? ? //小數(shù)點(diǎn)的處理。 ? ? ? ? ? ? ? if ((int)e.KeyChar == 46) ? ? ? ? ? ? ? ? ? ? ? ? ? //小數(shù)點(diǎn) ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? if (textBox1.Text.Length <= 0) ? ? ? ? ? ? ? ? ? ? ? e.Handled = true; ? //小數(shù)點(diǎn)不能在第一位 ? ? ? ? ? ? ? ? ? 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; ? //處理負(fù)數(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ù)點(diǎn)? ? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? ? 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ù)點(diǎn)? }
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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C#面向?qū)ο缶幊讨氯螒驅(qū)崿F(xiàn)方法
這篇文章主要介紹了C#面向?qū)ο缶幊讨氯螒驅(qū)崿F(xiàn)方法,以一個(gè)完整的猜拳游戲?yàn)槔v述了C#面向?qū)ο蟪绦蛟O(shè)計(jì)的具體實(shí)現(xiàn)步驟,具有一定的學(xué)習(xí)與借鑒價(jià)值,需要的朋友可以參考下2014-11-11C#自定義鼠標(biāo)拖拽Drag&Drop效果之基本原理及基本實(shí)現(xiàn)代碼
拖拽效果無(wú)論是在系統(tǒng)上、應(yīng)用上、還是在網(wǎng)頁(yè)上,拖拽隨處可見(jiàn),下面通過(guò)本文介紹下C#自定義鼠標(biāo)拖拽Drag&Drop效果之基本原理及基本實(shí)現(xiàn)代碼,需要的朋友可以參考下2022-04-04C#提高編程能力的50個(gè)要點(diǎn)總結(jié)
這篇文章主要介紹了C#提高編程能力的50個(gè)要點(diǎn),較為詳細(xì)的總結(jié)分析了C#程序設(shè)計(jì)中常見(jiàn)的注意事項(xiàng)與編程技巧,需要的朋友可以參考下2016-02-02