WinForm中限制只能輸入數(shù)字-創(chuàng)新互聯(lián)

在Winform(C#)中要實現(xiàn)限制Textbox只能輸入數(shù)字,一般的做法就是在按鍵事件中處理,判斷keychar的值。限制只能輸入數(shù)字,小數(shù)點,Backspace,del這幾個鍵。數(shù)字0~9所對應(yīng)的keychar為48~57,小數(shù)點是46,Backspace是8。
      拖一個Textbox到窗體上,添加OnKeyPress事件,在事件寫判斷的代碼,只要判斷不是這些鍵,設(shè)置e.Handled的值為true,就可以屏蔽輸入。WinForm中限制只能輸入數(shù)字

方法一:

成都創(chuàng)新互聯(lián)公司從2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站制作、網(wǎng)站建設(shè)網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元都昌做網(wǎng)站,已為上家服務(wù),為都昌各地企業(yè)和個人服務(wù),聯(lián)系電話:13518219792
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!=''&&!Char.IsDigit(e.KeyChar))
    {
      e.Handled= true;
    }

}

方法三:

private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar!='')//這是允許輸入退格鍵{
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+$"; 
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ù)點
}

本文題目:WinForm中限制只能輸入數(shù)字-創(chuàng)新互聯(lián)
文章來源:http://muchs.cn/article32/cdscsc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、移動網(wǎng)站建設(shè)、企業(yè)建站品牌網(wǎng)站制作、網(wǎng)站收錄、手機網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護公司