// Verify that the pressed key isn't CTRL or any non-numeric digit if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.')) { e.Handled = true; } // If you want, you can allow decimal (float) numbers if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1)) { e.Handled = true; }
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)) { e.Handled = true; }
if (textBox1.Text != "" && Int16.Parse(textBox1.Text) > 1000) { textBox1.Text = textBox1.Text.Substring(0, textBox1.TextLength - 1); MessageBox.Show("ใส่ตัวเลขระหว่าง 1-999 เท่านั้น", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (textBox1.Text == "") label1.Text = "ใส่ตัวเลขระหว่าง 1-999"; else { label1.Text = textBox1.Text + " x 2 = " + Int16.Parse(textBox1.Text)*2 ; }