日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#中如何限制TextBox控件內輸入值的范圍_C#教程

作者:Danny_hi ? 更新時間: 2023-03-20 編程語言

C#限制TextBox控件內輸入值的范圍

舉個例子

比如要限制TextBox1控件內只能輸入1~100的數字(先將TextBox1的MaxLength屬性設置成3):

1.首先要限制輸入的只能是數值,不能是字母或其他符號;選擇添加textBox1的KeyPress事件

代碼如下:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
? ? ? ? {
? ? ? ? ? ? if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
? ? ? ? ? ? ? ? e.Handled = true;
? ? ? ? }

2.再限制輸入數值的范圍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控件限制只允許輸入數字及小數點

//判斷按鍵是不是要輸入的類型。
?
? ? ? ? ? ? if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
?
? ? ? ? ? ? ? ? e.Handled = true;
?
?
? ? ? ? ? ? //小數點的處理。
?
? ? ? ? ? ? if ((int)e.KeyChar == 46) ? ? ? ? ? ? ? ? ? ? ? ? ? //小數點
?
? ? ? ? ? ? {
?
? ? ? ? ? ? ? ? if (textBox1.Text.Length <= 0)
?
? ? ? ? ? ? ? ? ? ? e.Handled = true; ? //小數點不能在第一位
?
? ? ? ? ? ? ? ? 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;
?
? ? ? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? }

處理只輸入數字的

方法一:?? ?

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; ? //處理負數?
? ? ? ? ? ? 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數字?
{?
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, "只能輸入數字!"); ?
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)//小數點?
? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? 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代表小數點?
}

C# 文本框只能輸入數字和退格鍵

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、空還是僅由空白字符組成。

總結

原文鏈接:https://blog.csdn.net/qq_43024228/article/details/86604365

欄目分類
最近更新