0

help pls i need someone that can help me when i enter the date of birth using the datetimepicker box it automatically compute the age and output in a textbox.text

4
Contributors
8
Replies
26
Views
6 Years
Discussion Span
Last Post by anaszaheerp
0
Dim myCoolBirthday As Date = DateTimePicker1.Value.Date '// selected value from DateTimePicker.
        Dim currentDate As Date = Today '// value of current date.
        Dim myAge As Integer = currentDate.Year - myCoolBirthday.Year '// subtract years from current date.
        MsgBox("My age: " & myAge & vbNewLine & "Just don't tell anyone. :)") '// display age.

Edited by codeorder: n/a

0

here is my code....
code for button..
Dim born, today As Integer
Dim now() As String
Dim now1 As String
now1 = Date.Today
now = now1.Split("/")
born = CInt(TextBox1.Text)
today = CInt(now(2))
born = today - born
TextBox2.Text = born

what i want is that i want to use the datetimepicker and it automatically compute the age in another textbox without clicking the button..

0

i change the code to textbox2.text = myAge but the problem is if i change the year in datetimepicker it does not compute anymore the age

0
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        Dim Age As Double = Math.Floor(DateDiff(DateInterval.Month, DateTimePicker1.Value, System.DateTime.Now) / 12)
        TextBox1.Text = "Your age is: " & Age
    End Sub

Edited by samuel_1991: Optimize 2 lines of code into 1 line just to declare the Age variable.

0

here is my code....
code for button..
Dim born, today As Integer
Dim now() As String
Dim now1 As String
now1 = Date.Today
now = now1.Split("/")
born = CInt(TextBox1.Text)
today = CInt(now(2))
born = today - born
TextBox2.Text = born

what i want is that i want to use the datetimepicker and it automatically compute the age in another textbox without clicking the button..

I have modified your code slightly, but for future references arjen, please refrain from using Variables that relate to vb.net.
Example, you declared now and today, which are both used by vb.net to access certain values.

TextBox1.Text = DateTimePicker1.Value.Year
        Dim now1 As String = Date.Today
        Dim now() As String = now1.Split("/")
        Dim today As Integer = CInt(now(2))
        Dim born As Integer = Today - CInt(TextBox1.Text)
        TextBox2.Text = born

Edited by codeorder: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.