Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a textBox accept only Numbers and just one decimal point in Windows 8

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) {

}
like image 903
Dikainc Avatar asked Dec 12 '22 10:12

Dikainc


1 Answers

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);
        }
like image 199
beginnner Avatar answered Jan 26 '23 00:01

beginnner