C# TextBox中只能輸入數位幾種常用方法(C#)

來源:互聯網
上載者:User
TextBox中只能輸入數位幾種常用方法(C#)

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 button1_Click(object sender, EventArgs e)
{
string text = this.textBox1.Text;
if (text != null)
MessageBox.Show(text);
}

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

來自:http://bbs.bccn.net/thread-205138-1-1.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.