Help me :)
how to auto calculate the age, when you type in MaskedTextBox1 using Birtdate?

Recommended Answers

All 5 Replies

As long as you have verified that the value entered is a valid date and is less than or equal to the current date then convert that value to a date and calculate the difference in years between that date and now. Then you have to subtract one if the birthday falls later than the current date.

Dim birth As Date = CDate(MaskedTextBox1.Text)
Dim age As Integer = DateDiff(DateInterval.Year, birth, Now)

If birth.AddYears(age) > Now Then
    age -= 1
End If

You could tie that to the Textchanged event or the validation event to have the calculation done automatically.

You could tie that to the Textchanged event

You wouldn't want to do that because it fires on every keystroke and you don't want to do the calculation until the entire date has been entered.

You wouldn't want to do that because it fires on every keystroke and you don't want to do the calculation until the entire date has been entered.

Unless you also wanted on the fly validation before the calculation, then do the calculation on a specific keystroke.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.