How to set a random date in the .net app's month calendar object using QTP? The window does not have any input box to insert the date directly. The class name of that calendar UIAObject is WindowsForms10.SysMonthCal32.
Asked
Active
Viewed 119 times
1 Answers
1
You can get the native window handle and send MCM_SETSELRANGE
message to set the selected date for MonthCalendar
.
To do so, I suppose you have found the element, then you can use follwing code:
var date = new DateTime(1998, 1, 1);
MonthCalendarHelper.SetDate((IntPtr)element.Current.NativeWindowHandle, date);
MonthCalendarHelper
Here is the source code for MonthCalendarHelper
. The class has two public static methods which allow you to set a date range or a selected date for the month calendar control:
using System;
using System.Runtime.InteropServices;
public class MonthCalendarHelper
{
const int MCM_SETSELRANGE = (0x1000 + 6);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct SYSTEMTIMEARRAY
{
public short wYear1;
public short wMonth1;
public short wDayOfWeek1;
public short wDay1;
public short wHour1;
public short wMinute1;
public short wSecond1;
public short wMilliseconds1;
public short wYear2;
public short wMonth2;
public short wDayOfWeek2;
public short wDay2;
public short wHour2;
public short wMinute2;
public short wSecond2;
public short wMilliseconds2;
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, int msg,
int wParam, SYSTEMTIMEARRAY lParam);
public static void SetDate(IntPtr handle, DateTime date)
{
SetDateRange(handle, date, date);
}
public static void SetDateRange(IntPtr handle, DateTime start, DateTime end)
{
var value = new SYSTEMTIMEARRAY();
value.wYear1 = (short)start.Year;
value.wMonth1 = (short)start.Month;
value.wDayOfWeek1 = (short)start.DayOfWeek;
value.wDay1 = (short)start.Day;
value.wYear2 = (short)end.Year;
value.wMonth2 = (short)end.Month;
value.wDayOfWeek2 = (short)end.DayOfWeek;
value.wDay2 = (short)end.Day;
SendMessage(handle, MCM_SETSELRANGE, 0, value);
}
}

Reza Aghaei
- 120,393
- 18
- 203
- 398
-
Thanks for your quick response. Will check it out. – koder Feb 16 '18 at 21:34