Set DateTimePicker element via AutomationElement
You can get the native window handle and send DTM_SETSYSTEMTIME
message to set the selected date for DateTimePicker
control.
To do so, I suppose you have found the element, then you can use follwing code:
var date = new DateTime(1998, 1, 1);
DateTimePickerHelper.SetDate((IntPtr)element.Current.NativeWindowHandle, date);
DateTimePickerHelper
Here is the source code for DateTimePickerHelper
. The class has a public static SetDate
method which allow you to set a date for the date time picker control:
using System;
using System.Runtime.InteropServices;
public class DateTimePickerHelper {
const int GDT_VALID = 0;
const int DTM_SETSYSTEMTIME = (0x1000 + 2);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct SYSTEMTIME {
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, int msg,
int wParam, SYSTEMTIME lParam);
public static void SetDate(IntPtr handle, DateTime date) {
var value = new SYSTEMTIME() {
wYear = (short)date.Year,
wMonth = (short)date.Month,
wDayOfWeek = (short)date.DayOfWeek,
wDay = (short)date.Day,
wHour = (short)date.Hour,
wMinute = (short)date.Minute,
wSecond = (short)date.Second,
wMilliseconds = 0
};
SendMessage(handle, DTM_SETSYSTEMTIME, 0, value);
}
}