Reputation: 1371
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 :( ?
Upvotes: 4
Views: 1483
Reputation: 387
I came here because I had the same problem.
I've tried setting the DateTimePicker Date via:
You have to focus the application window, then the DateTimePicker and then enter the date using the SendKeys.SendWait method.
Sources:
Here's an example code that uses the FlaUI package:
// This code uses the FlaUI package to retrieve the element
var automationId = "AUTOMATION ID OF YOUR DATETIME PICKER";
var process = Process.GetProcesses().First(p => p.MainWindowTitle.Contains("YOUR APPLICATION WINDOW TITLE"));
var application = Application.Attach(process.Id);
var appWindow = application.GetMainWindow(new UIA3Automation(), TimeSpan.FromSeconds(10));
var dateTimePicker = appWindow.FindFirstDescendant(cf => cf.ByAutomationId(automationId));
var targetTime = DateTime.Parse("13.04.2024 08:00");
appWindow.Focus();
datePicker.Focus();
SendKeys.SendWait("^{HOME}");
SendKeys.SendWait($"{targetTime:dd}");
SendKeys.SendWait("{RIGHT}");
SendKeys.SendWait($"{targetTime:MM}");
SendKeys.SendWait("{RIGHT}");
SendKeys.SendWait($"{targetTime:yyyy}");
SendKeys.SendWait("{RIGHT}");
SendKeys.SendWait($"{targetTime:HH}");
SendKeys.SendWait("{RIGHT}");
SendKeys.SendWait($"{targetTime:ss}");
I am aware that the example code can be simplified, but this is how it worked on my machine.
Therefore, I posted it here as it is. I hope this helps.
Upvotes: 0
Reputation: 10424
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);
}
Upvotes: 0
Reputation: 125197
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);
}
}
Upvotes: 1