I want to check the number format as the user types in each number in the following format
Three digits before decimal and one digit after point(if any)
The moment the user enters the 3 digits, Im trying to add decimal point. Is there any event which is fired when user enters a number?
The TextInput and TextInputStart events do not work as expected. When i try to enter 332 it shows as 233. The following function is called on the TextInputStart event.
private void TestFunction(object sender, TextCompositionEventArgs e)
{
TextBox txtbox = e.OriginalSource as TextBox;
string r = txtbox.Text;
if(r.Contains('.'))
{
for (int i = 0; i < r.Length; i++)
{
if (r.Substring(i, 1) == ".")
{
txtbox.Text = r.Substring(0, i + 2);
}
}
}
if (r.Length == 2 && r[2] != '.')
{
r += ".";
txtbox.Text = r;
}
}
While debuggin i noticed that, TextInputStart is fired and the text box has the previous string and not the last entered string.
Any way out? 🙁
I used regular expressions to check 3 digits before decimal and one digit after decimal. The following function is called on the key up event of the textbox. Saves a lot of time and can avoid the unnecessary use of messagebox. If anybody is looking out for an answer, here is mine.