How to set TextBox to only accept numbers?
-
I have already checked other questions here but the answers are not related to my issue. the following code allows textbox1 to only accept numbers if the physical keyboard (laptop) is pressed: private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { char ch = e.KeyChar; if ( !char.IsDigit(ch)) { e.Handled = true; } } but this is not what I wanted (I dont use physical laptop keyboard). I have windows form with buttons and a textbox1. I designed keyboard and it works well but I want textbox1 to only accept numbers and the ".". There are only two lines of code inside each button (and only code in the project) which is: private void buttonName_Click(object sender, EventArgs e) { // each button only has this code. textBox1.Focus(); SendKeys.Send(buttonName.Text); } I know how to set textbox to accept numbers if the physical (laptop ) keys are pressed but here in this case I have windows form with control buttons ( each button to print its text into textbox1) and I want to set textBox1 to only accept numbers and the ".". Please help in how to achieve this. Thank you
-
I have already checked other questions here but the answers are not related to my issue. the following code allows textbox1 to only accept numbers if the physical keyboard (laptop) is pressed: private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { char ch = e.KeyChar; if ( !char.IsDigit(ch)) { e.Handled = true; } } but this is not what I wanted (I dont use physical laptop keyboard). I have windows form with buttons and a textbox1. I designed keyboard and it works well but I want textbox1 to only accept numbers and the ".". There are only two lines of code inside each button (and only code in the project) which is: private void buttonName_Click(object sender, EventArgs e) { // each button only has this code. textBox1.Focus(); SendKeys.Send(buttonName.Text); } I know how to set textbox to accept numbers if the physical (laptop ) keys are pressed but here in this case I have windows form with control buttons ( each button to print its text into textbox1) and I want to set textBox1 to only accept numbers and the ".". Please help in how to achieve this. Thank you
-
I have already checked other questions here but the answers are not related to my issue. the following code allows textbox1 to only accept numbers if the physical keyboard (laptop) is pressed: private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { char ch = e.KeyChar; if ( !char.IsDigit(ch)) { e.Handled = true; } } but this is not what I wanted (I dont use physical laptop keyboard). I have windows form with buttons and a textbox1. I designed keyboard and it works well but I want textbox1 to only accept numbers and the ".". There are only two lines of code inside each button (and only code in the project) which is: private void buttonName_Click(object sender, EventArgs e) { // each button only has this code. textBox1.Focus(); SendKeys.Send(buttonName.Text); } I know how to set textbox to accept numbers if the physical (laptop ) keys are pressed but here in this case I have windows form with control buttons ( each button to print its text into textbox1) and I want to set textBox1 to only accept numbers and the ".". Please help in how to achieve this. Thank you
You can use TextChanged event as Gerry said. Then you can verify the text using this method.
public static bool IsNumber(string s)
{
var r = new Regex(@"^-?[0-9]\d*(\.\d+)?$");
return r.Match(s).Success;
}BTW it also accept negative numbers
-
I have already checked other questions here but the answers are not related to my issue. the following code allows textbox1 to only accept numbers if the physical keyboard (laptop) is pressed: private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { char ch = e.KeyChar; if ( !char.IsDigit(ch)) { e.Handled = true; } } but this is not what I wanted (I dont use physical laptop keyboard). I have windows form with buttons and a textbox1. I designed keyboard and it works well but I want textbox1 to only accept numbers and the ".". There are only two lines of code inside each button (and only code in the project) which is: private void buttonName_Click(object sender, EventArgs e) { // each button only has this code. textBox1.Focus(); SendKeys.Send(buttonName.Text); } I know how to set textbox to accept numbers if the physical (laptop ) keys are pressed but here in this case I have windows form with control buttons ( each button to print its text into textbox1) and I want to set textBox1 to only accept numbers and the ".". Please help in how to achieve this. Thank you
Assuming this is Windows Forms, and there's no physical keyboard:
private void NumberButtons_Click(object sender, EventArgs e)
{
textBox1.AppendText((sender as Button).Text);
}The assumption here is that you have a set of Buttons whose Text values are from #1~9 and #0, and they all use the same Click EventHandler. Of course, we don't know here exactly what you mean by "numbers:" are you allowing decimal-point, commas, negative numbers, culture-specific numeric encoding, etc. ? Will also have a back-space button ? If you wish the user to have some way to move the insertion point around in the TextBox, that'll take a bit more work (using virtual arrow-key buttons ?).
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.