Problem with my numeric text box user control
-
Hai Me with C#.net I want to make a text box to accept only numbers and decimal points(decimal point only once), so I tried to make a user control and is working fine except one problem. If if select all the data in the text box and try to enter a new value decimal point will not accept until I press the backspace. My user control code is a follows string i; int f; public string Text { get { return NMTextBox.Text; } set { NMTextBox.Text = value; } } private void NMTextBox_KeyPress(object sender, KeyPressEventArgs e) { try { string numaric = NMTextBox.Text; for (int j = 0; j < numaric.Length; j++) { char myChar = numaric[j]; } if (NMTextBox.Text == "") { f = 0; } if (e.KeyChar.ToString() == ".") { ++f; if (f >= 2) { e.Handled = true; } } if (i == "8") { f = 0; for (int j = 0; j < numaric.Length - 1; j++) { if (numaric[j].ToString() == ".") { ++f; } } } if (e.KeyChar.ToString() != "." && Char.IsNumber(e.KeyChar) != true) { if (i != "8") { e.Handled = true; } } } catch (Exception ex) { MessageBox.Show(ex.StackTrace, ex.Message.ToString()); } } private void NMTextBox_KeyDown(object sender, KeyEventArgs e) { i = e.KeyValue.ToString(); }
Thanks & Regards