網站首頁 編程語言 正文
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
相關推薦
- 2022-09-10 Python自動打印被調用函數變量名及對應值?_python
- 2022-08-06 Python結合spaCy?進行簡易自然語言處理_python
- 2022-02-24 開機時自動運行批處理
- 2022-06-18 asp.net中Log4.net的工具類helper_實用技巧
- 2022-04-28 WPF使用WrapPanel環繞面板布局_實用技巧
- 2022-07-16 Spring MVC @ModelAttribute注解
- 2023-03-28 python?list與numpy數組效率對比_python
- 2023-03-25 iOS13適配三指撤銷和文案限長實例詳解_IOS
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支