Click here to Skip to main content
15,997,776 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi i want to set min and max date of datetimepicker from selected year.

Thanks in Advance

What I have tried:

My Code:
VB
Public Structure SYSTEMTIME
        Dim wYear As Integer
        Dim wMonth As Integer
        Dim wDayOfWeek As Integer
        Dim wDay As Integer
        Dim wHour As Integer
        Dim wMinute As Integer
        Dim wSecond As Integer
        Dim wMilliseconds As Integer

    End Structure

    Public Declare Function SendMessage Lib "user32.dll" _
                         Alias "SendMessageA" _
                         (ByVal hWnd As IntPtr, ByVal wMsg As Int32, ByVal wParam As Integer, ByVal lParam As SYSTEMTIME) As IntPtr
    Public Const DTM_SETRANGE = &H1004&
    Public Const GDTR_MIN = 1
    Public Const GDTR_MAX = 2

  Dim TimeArray(1) As SYSTEMTIME
        Dim result As IntPtr
        'Dim trdt As Date

        ' Define first minimum date.
        TimeArray(0).wDay = Format(dt, "dd")
        TimeArray(0).wMonth = Format(dt, "MM")
        TimeArray(0).wYear = Format(dt, "yyyy")

        ' Define second maximum date.
        TimeArray(1).wDay = Format(Dt1, "dd")
        TimeArray(1).wMonth = Format(Dt1, "MM")
        TimeArray(1).wYear = Format(Dt1, "yyyy")

        result = SendMessage(Me.dtpFrom.Handle, DTM_SETRANGE,
   GDTR_MIN + GDTR_MAX, TimeArray(0))

Its showing error "Attempted to read or write protected memory. This is often an indication that other memory is corrupt
Posted
Updated 5-Sep-18 9:22am
v2

1 solution

Why? The DateTimePicker class[^] already has MaxDate[^] and MinDate[^] properties. Just use those.
VB.NET
Me.dtpFrom.MinDate = dt
Me.dtpFrom.MaxDate = dt1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900