LOOP question

cesarfrancisco

Active member
Joined
Apr 3, 2007
Messages
33
Programming Experience
Beginner
'Calculate age based today's and the person's birthday. Birthday can be entered like (2 2 45), (1-31-1945), 2/28/45)
PrivateSub txtboxDOB_TextChanged(ByVal sender AsObject, ByVal e As System.EventArgs) Handles txtboxDOB.TextChanged
Dim sb AsNew System.Text.StringBuilder
Dim pos AsInteger = txtboxDOB.SelectionStart
ForEach c AsCharIn txtboxDOB.Text
SelectCase c
CaseChar.Parse("/")
sb.Append(c)
CaseChar.Parse("-")
sb.Append(c)
CaseChar.Parse(" ")
sb.Append(c)
CaseElse
IfChar.IsDigit(c) Then
sb.Append(c)
EndIf
EndSelect
Next
txtboxDOB.Text = sb.ToString
txtboxDOB.SelectionStart = pos

Dim BirthDate As DateTime
If DateTime.TryParse(txtboxDOB.Text, BirthDate) Then
BirthDate = Date.Parse(txtboxDOB.Text)
EndIf

Dim age AsInteger
If ((DateTime.Now.Month + DateTime.Now.Day / 100) < (BirthDate.Month + BirthDate.Day / 100))
age = (DateTime.Now.Year - BirthDate.Year) - 1
Else
age = (DateTime.Now.Year - BirthDate.Year)
EndIf
txtboxAge.Text = age.ToString
If age = 0 Then
txtboxAge.Text = "<1"
ElseIf age < 0 Then
txtboxAge.Text = "?"
ElseIf age > 110 Then
txtboxAge.Text = "?"
EndIf
EndSub

The problem appears when the user started to type in a DOB but relents. Now the cursor is stuck. How to remedy this?
 
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Table1TableAdapter.Fill(Me.NewAppDBDataSet.Table1)
Table1BindingSource.AddNew()
End Sub

I found the problem:
The form was filled then a new empty datarow was added.
This somehow interfere with the code above.
 
It's default validation behaviour; a string can accept anything, an Integer can only accept (something that can be parsed into an integer), a Date can only accept (something that can be parsed into) a date

i.e. if you type a bunch of crap into an integer-bound text field, and CausesValidation is set to true for that textbox, then you cannot leave the box until you type something sensible
 
Back
Top