I am new to windows 8 phone. I am writing a calculator app that can only accept numbers in the textbox and just a single decimal point. how do I prevent users from inputting two or more decimal Points in the text box as the calculator cant handle that.
I have been using Keydown Event, is that the best or should I use Key up?
private void textbox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) {
}
You can use this for KeyPress Event set keypress event for your textbox in form.Designer like this
this.yourtextbox.KeyPress +=new System.Windows.Forms.KeyPressEventHandler(yourtextbox_KeyPress);
then use it in your form
//only number And single decimal point input فقط عدد و یک ممیز میتوان نوشت
public void onlynumwithsinglepoint(object sender, KeyPressEventArgs e)
{
if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == '.'))
{ e.Handled = true; }
TextBox txtDecimal = sender as TextBox;
if (e.KeyChar == '.' && txtDecimal.Text.Contains("."))
{
e.Handled = true;
}
}
then
private void yourtextbox_KeyPress(object sender, KeyPressEventArgs e)
{
onlynumwithsinglepoint(sender, e);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With