Skip to main content

DateTime Picker control in VB.NET

Public Class Form1
    Dim flag As Short = 0
    Dim d1, d2 As DateTime
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        flag = 1
        Button1.FlatStyle = FlatStyle.Flat
        Button2.FlatStyle = FlatStyle.Standard
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        flag = 2
        Button2.FlatStyle = FlatStyle.Flat
        Button1.FlatStyle = FlatStyle.Standard
    End Sub
    Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
        If flag = 1 Then
            d1 = DateTimePicker1.Value
            Label1.Text = DateTimePicker1.Value.Date
        ElseIf flag = 2 Then
            d2 = DateTimePicker1.Value
            Label2.Text = DateTimePicker1.Value.Date
        Else
            MsgBox("Select Birthdate or Now Date")
        End If
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim result As TimeSpan = d2.Subtract(d1)
        Dim day = result.TotalDays
        Dim year = day / 365
        Dim mon = year * 12
        MsgBox("Total Days :" & day & vbNewLine & "Total Months :" & Math.Round(mon, 0) & vbNewLine & "Year : " & Math.Round(year, 0))
    End Sub
End Class
DESIGNING




Comments