4

I want to be able to set a DateTimePicker element to a certain time via AutomationElement. It stores the time as "hh:mm:ss tt" (i.e. 10:45:56 PM).

I get the element as such:

ValuePattern p = AECollection[index].GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;

I believe I have two options:

p.SetValue("9:41:22 AM");

or

p.Current.Value = "9:41:22 AM";

However, the first option simply doesn't work (I read somewhere that this may be broken in .NET 2.0?, I am using .NET 3.0 though). The second option tells me that the element is read only, how can I change the status so that it is not read only? Or more simply, how can I change the time :( ?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
jpints14
  • 1,361
  • 6
  • 15
  • 24

2 Answers2

1

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);
    }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
0

This solution works for Wpf based app

object patternObj = AECollection[index].GetCurrentPattern(UIA.UIA_PatternIds.UIA_ValuePatternId);
if (patternObj != null) {
 (UIA.IUIAutomationValuePattern)patternObj.SetValue(itemVal);
}
Rajesh
  • 10,318
  • 16
  • 44
  • 64