Good Day Programmer's ^_^

i'm having a problem with this

two DateTimePicker

in DateTimePicker1 the format or value is "dd-MMM-yyyy" or "01/Jan/2012"

and in DateTimePicker2 the format or value is "yyyy" or "2012"

when i change the year in DateTimePicker2 to change the year in DateTimePicker1

i'm getting an error..

how do i change only the Year in DateTimePicker1 base on the DateTimePicker2 that i change?? that the DATE on DateTimePicker1 will not change, how to do that??

i'm getting an error..

Next time post the error message.

You set the formats like this

DateTimePicker1.Format = DateTimePickerFormat.Custom
DateTimePicker1.CustomFormat = "dd-MMM-yyyy"
DateTimePicker2.Format = DateTimePickerFormat.Custom
DateTimePicker2.CustomFormat = "yyyy"

then you trap DateTimePicker2's ValueChanged event, right?

Private Sub DateTimePicker2_ValueChanged(sender As Object, e As System.EventArgs) Handles DateTimePicker2.ValueChanged
    '
    Dim newYear As Integer
    Dim oldYear As Integer
    Dim oldMonth As Integer
    Dim oldDay As Integer

    ' Get year
    newYear = CType(sender, DateTimePicker).Value.Year
    ' or just: newYear = DateTimePicker2.Value.Year
    ' Get current year, month and day
    oldYear = DateTimePicker1.Value.Year
    oldMonth = DateTimePicker1.Value.Month
    oldDay = DateTimePicker1.Value.Day
    ' Check leap year and if this is the leap day
    If DateTime.IsLeapYear(oldYear) AndAlso oldMonth = 2 AndAlso oldDay = 29 Then
        ' If new year is not leap year then February has 28 days
        If Not DateTime.IsLeapYear(newYear) Then
            oldDay = 28
        End If
    End If
    ' Create a new date value
    DateTimePicker1.Value = New DateTime(newYear, oldMonth, oldDay)

End Sub

You have to check for the leap year yourself. For example 2/29/2012 won't be automatically changed to 2/28/2013 if you set the new year to 2013 which is not a leap year.

HTH

i'm sorry...

i already solve my problem...

i only used one line of code...

this:
DateTimePicker1.Value = "01/JAN/" & DateTimePicker2.Value.Year

sorry again if i'm not posting the error message....

>>DateTimePicker1.Value = "01/JAN/" & DateTimePicker2.Value.Year
If you do not need to constantly change it to Jan.1st, see if this helps.

With DateTimePicker1
                .Value = New Date(DateTimePicker2.Value.Year, .Value.Month, .Value.Day)
            End With
commented: satisfied? +1
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.