Best FlaUI code snippet using FlaUI.Core.AutomationElements.Calendar.SetSelectedDate
Win32Fallback.cs
Source:Win32Fallback.cs  
...336                    0, 0, 0);337            }338            return datetime;339        }340        internal static void SetSelectedDate(IntPtr handle, DateTime date)341        {342            if (handle == IntPtr.Zero || GetWindowClassName(handle) != "SysMonthCal32")343            {344                throw new Exception("Not supported for this type of calendar");345            }346            uint styles = User32.GetWindowLong(handle, WindowLongParam.GWL_STYLE);347            if ((styles & Win32CalendarStyles.MCS_MULTISELECT) != 0)348            {349                // multiselect calendar350                SetSelectedRange(handle, new DateTime[] { date, date });351                return;352            }353            uint procid = 0;354            User32.GetWindowThreadProcessId(handle, out procid);355            IntPtr hProcess = User32.OpenProcess(ProcessAccessFlags.All, false, (int)procid);356            if (hProcess == IntPtr.Zero)357            {358                throw new Exception("Insufficient rights");359            }360            SYSTEMTIME systemtime = new SYSTEMTIME();361            systemtime.Year = (short)date.Year;362            systemtime.Month = (short)date.Month;363            systemtime.Day = (short)date.Day;364            systemtime.DayOfWeek = (short)date.DayOfWeek;365            systemtime.Hour = (short)date.Hour;366            systemtime.Minute = (short)date.Minute;367            systemtime.Second = (short)date.Second;368            systemtime.Milliseconds = (short)date.Millisecond;369            // allocate memory in the process of the calendar370            IntPtr hMem = User32.VirtualAllocEx(hProcess, IntPtr.Zero, (uint)Marshal.SizeOf(systemtime),371                AllocationType.Commit | AllocationType.Reserve, MemoryProtection.ReadWrite);372            if (hMem == IntPtr.Zero)373            {374                throw new Exception("Insufficient rights");375            }376            IntPtr lpNumberOfBytesWritten = IntPtr.Zero;377            if (User32.WriteProcessMemory(hProcess, hMem, systemtime, Marshal.SizeOf(systemtime), out lpNumberOfBytesWritten) == false)378            {379                throw new Exception("Insufficient rights");380            }381            User32.SendMessage(handle, Win32CalendarMessages.MCM_SETCURSEL, IntPtr.Zero, hMem);382            // release memory383            User32.VirtualFreeEx(hProcess, hMem, Marshal.SizeOf(systemtime),384                AllocationType.Decommit | AllocationType.Release);385            User32.CloseHandle(hProcess);386        }387        // Selects a range in a multiple selection Win32 calendar. The range is specified by the first and the last date.388        // If the calendar is single selection then the second date will be selected.389        // The "dates" parameter should always contain two dates.390        internal static void SetSelectedRange(IntPtr handle, DateTime[] dates)391        {392            if (handle == IntPtr.Zero || GetWindowClassName(handle) != "SysMonthCal32")393            {394                throw new Exception("Not supported for this type of calendar");395            }396            if (dates.Length != 2)397            {398                throw new Exception("Dates array length must be 2");399            }400            uint styles = User32.GetWindowLong(handle, WindowLongParam.GWL_STYLE);401            if ((styles & Win32CalendarStyles.MCS_MULTISELECT) == 0)402            {403                // singleselect calendar404                SetSelectedDate(handle, dates[1]);405                return;406            }407            uint procid = 0;408            User32.GetWindowThreadProcessId(handle, out procid);409            IntPtr hProcess = User32.OpenProcess(ProcessAccessFlags.All, false, (int)procid);410            if (hProcess == IntPtr.Zero)411            {412                throw new Exception("Insufficient rights");413            }414            SYSTEMTIME systemtime1 = new SYSTEMTIME();415            systemtime1.Year = (short)dates[0].Year;416            systemtime1.Month = (short)dates[0].Month;417            systemtime1.Day = (short)dates[0].Day;418            systemtime1.DayOfWeek = (short)dates[0].DayOfWeek;...Calendar.cs
Source:Calendar.cs  
...68        public void SelectDate(DateTime date)69        {70            if (FrameworkType == FrameworkType.Wpf)71            {72                SetSelectedDate(date, false);73                return;74            }75            else if (FrameworkType == FrameworkType.Win32)76            {77                if (Properties.NativeWindowHandle.IsSupported)78                {79                    var windowHandle = Properties.NativeWindowHandle.ValueOrDefault;80                    if (windowHandle != IntPtr.Zero)81                    {82                        Win32Fallback.SetSelectedDate(windowHandle, date);83                        return;84                    }85                }86            }8788            throw new NotSupportedException("Not supported");89        }9091        /// <summary>92        /// For WPF calendar with SelectionMode="MultipleRange" this method deselects other selected dates and selects the specified range.93        /// For any other type of SelectionMode it deselects other selected dates and selects only the last date in the range.94        /// For Win32 multiple selection calendar the "dates" parameter should contain two dates, the first and the last date of the range to be selected.95        /// For Win32 single selection calendar this method selects only the second date from the "dates" array.96        /// For WPF calendar all dates should be specified in the "dates" parameter, not only the first and the last date of the range.97        /// </summary>98        public void SelectRange(DateTime[] dates)99        {100            if (dates.Length == 0)101            {102                return;103            }104105            if (FrameworkType == FrameworkType.Wpf)106            {107                SetSelectedDate(dates[0], false);108                for (int i = 1; i < dates.Length; i++)109                {110                    SetSelectedDate(dates[i], true);111                }112                return;113            }114            else if (FrameworkType == FrameworkType.Win32)115            {116                if (Properties.NativeWindowHandle.IsSupported)117                {118                    var windowHandle = Properties.NativeWindowHandle.ValueOrDefault;119                    if (windowHandle != IntPtr.Zero)120                    {121                        Win32Fallback.SetSelectedRange(windowHandle, dates);122                        return;123                    }124                }125            }126127            throw new NotSupportedException("Not supported");128        }129130        /// <summary>131        /// For WPF calendar with SelectionMode="MultipleRange" this method adds the specified date to current selection.132        /// For any other type of SelectionMode it deselects other selected dates and selects the specified date.133        /// This method is supported only for WPF calendar.134        /// </summary>135        public void AddToSelection(DateTime date)136        {137            SetSelectedDate(date, true);138        }139140        /// <summary>141        /// For WPF calendar with SelectionMode="MultipleRange" this method adds the specified range to current selection.142        /// For any other type of SelectionMode it deselects other selected dates and selects only the last date in the range.143        /// This method is supported only for WPF calendar.144        /// </summary>145        public void AddRangeToSelection(DateTime[] dates)146        {147            foreach (DateTime date in dates)148            {149                SetSelectedDate(date, true);150            }151        }152153        private void SetSelectedDate(DateTime date, bool add)154        {155            if (FrameworkType == FrameworkType.Wpf)156            {157                // switch to Decade view158                if (Patterns.MultipleView.TryGetPattern(out var multipleViewPattern))159                {160                    int[] views = multipleViewPattern.SupportedViews;161                    if (views.Length >= 3)162                    {163                        // the third view is the Decade view164                        multipleViewPattern.SetCurrentView(views[2]);165                    }166                }167
...SetSelectedDate
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using FlaUI.Core.AutomationElements;7using FlaUI.Core.AutomationElements.Infrastructure;8using FlaUI.Core.Definitions;9using FlaUI.Core.Input;10using FlaUI.Core.WindowsAPI;11using FlaUI.Core;12using FlaUI.UIA3;13using System.Windows.Forms;14using System.Threading;15{16    {17        static void Main(string[] args)18        {19            using (var automation = new UIA3Automation())20            {21                var app = FlaUI.Core.Application.Launch(@"C:\Windows\System32\calc.exe");22                var mainWindow = app.GetMainWindow(automation);23                Thread.Sleep(3000);24                var calendar = mainWindow.FindFirstDescendant(cf => cf.ByAutomationId("131"));25                calendar.AsCalendar().SetSelectedDate(DateTime.Now);26            }27        }28    }29}SetSelectedDate
Using AI Code Generation
1using FlaUI.Core.AutomationElements;2using FlaUI.Core.AutomationElements.Infrastructure;3using FlaUI.Core.Definitions;4using FlaUI.Core.Tools;5using System;6using System.Collections.Generic;7using System.Linq;8using System.Text;9using System.Threading.Tasks;10{11    {12        static void Main(string[] args)13        {14            var app = FlaUI.Core.Application.Launch(@"C:\Windows\System32\calc.exe");15            var automation = FlaUI.Core.Automation.AutomationFactory.GetAutomation();16            var window = app.GetMainWindow(automation);17            var button = window.FindFirstDescendant(cf => cf.ByAutomationId("133")).AsButton();18            button.Invoke();19            var calendar = window.FindFirstDescendant(cf => cf.ByAutomationId("121")).AsCalendar();20            calendar.SetSelectedDate(DateTime.Now);21            app.Close();22        }23    }24}SetSelectedDate
Using AI Code Generation
1using FlaUI.Core.AutomationElements;2using FlaUI.Core.AutomationElements.Infrastructure;3using FlaUI.Core.Definitions;4using FlaUI.Core.Tools;5using FlaUI.UIA3;6using System;7using System.Collections.Generic;8using System.Linq;9using System.Text;10using System.Threading.Tasks;11{12    {13        static void Main(string[] args)14        {15            var app = Application.Launch("notepad.exe");16            var automation = new UIA3Automation();17            var window = app.GetMainWindow(automation, TimeSpan.FromSeconds(10));18            var button = window.FindFirstDescendant(cf => cf.ByControlType(ControlType.Button)).AsButton();19            button.Click();20            var calendar = window.FindFirstDescendant(cf => cf.ByControlType(ControlType.Calendar)).AsCalendar();21            calendar.SetSelectedDate(new DateTime(2016, 1, 1));22            app.Close();23        }24    }25}SetSelectedDate
Using AI Code Generation
1using System;2using System.Windows.Automation;3using FlaUI.Core;4using FlaUI.Core.AutomationElements;5using FlaUI.Core.Conditions;6using FlaUI.Core.Definitions;7using FlaUI.Core.Input;8using FlaUI.Core.Tools;9using FlaUI.UIA3;10{11    {12        static void Main(string[] args)13        {14            using (var app = Application.Launch("calc.exe"))15            {16                Wait.UntilInputIsProcessed();17                var automation = new UIA3Automation();18                var window = app.GetMainWindow(automation);19                var button = window.FindFirstDescendant(cf => cf.ByText("Date Calculation"));20                button.Click();21                var win = window.FindFirstDescendant(cf => cf.ByControlType(ControlType.Window).And(cf.ByText("Date Calculation")));22                var calendar = win.FindFirstDescendant(cf => cf.ByControlType(ControlType.Calendar));23                calendar.AsCalendar().SetSelectedDate(new DateTime(2019, 1, 1));24                Wait.UntilInputIsProcessed();25            }26        }27    }28}SetSelectedDate
Using AI Code Generation
1using FlaUI.Core.AutomationElements;2using FlaUI.Core.AutomationElements.Infrastructure;3using FlaUI.Core.Definitions;4using FlaUI.Core.Tools;5using System;6using System.Collections.Generic;7using System.Linq;8using System.Text;9using System.Threading.Tasks;10{11    {12        static void Main(string[] args)13        {14            var application = Application.Launch("calc.exe");15            var mainWindow = application.GetMainWindow(Automation);16            var button = mainWindow.FindFirstDescendant(cf => cf.ByAutomationId("132")).AsButton();17            button.Click();18            var calendar = mainWindow.FindFirstDescendant(cf => cf.ByAutomationId("135")).AsCalendar();19            calendar.SetSelectedDate(new DateTime(2011, 11, 11));20            application.Close();21        }22    }23}24using FlaUI.Core.AutomationElements;25using FlaUI.Core.AutomationElements.Infrastructure;26using FlaUI.Core.Definitions;27using FlaUI.Core.Tools;28using System;29using System.Collections.Generic;30using System.Linq;31using System.Text;32using System.Threading.Tasks;33{34    {35        static void Main(string[] args)36        {37            var application = Application.Launch("calc.exe");38            var mainWindow = application.GetMainWindow(Automation);39            var button = mainWindow.FindFirstDescendant(cf => cf.ByAutomationId("132")).AsButton();40            button.Click();41            var calendar = mainWindow.FindFirstDescendant(cf => cf.ByAutomationId("135")).AsCalendar();42            calendar.SetSelectedDates(newSetSelectedDate
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using FlaUI.Core.AutomationElements;7using FlaUI.Core.AutomationElements.Infrastructure;8using FlaUI.Core.Definitions;9using FlaUI.Core.Input;10using FlaUI.Core.Tools;11using FlaUI.UIA3;12{13    {14        static void Main(string[] args)15        {16            var app = FlaUI.UIA3.Application.Launch(@"C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE");17            var mainWindow = app.GetMainWindow(FlaUI.Core.Automation.AutomationFactory.ByProcessId(app.ProcessId));18            var calendar = mainWindow.FindFirstDescendant(cf => cf.ByAutomationId("DatePicker")).AsCalendar();19            calendar.SetSelectedDate(2018, 12, 31);SetSelectedDate
Using AI Code Generation
1using System;2using FlaUI.Core;3using FlaUI.Core.AutomationElements;4using FlaUI.Core.AutomationElements.Infrastructure;5using FlaUI.Core.Definitions;6using FlaUI.Core.Input;7using FlaUI.Core.WindowsAPI;8using FlaUI.UIA3;9using System.Windows.Automation;10using System.Windows.Automation.Text;11using System.Threading;12using System.Windows.Forms;13using System.Windows.Automation.Provider;14using System.Collections.Generic;15using System.Linq;16using System.Text;17using System.Threading.Tasks;18{19    {20        static void Main(string[] args)21        {22            var app = Application.Launch(@"C:\Windows\System32\calc.exe");23            var automation = new UIA3Automation();24            var window = app.GetMainWindow(automation);25            var calendar = window.FindFirstDescendant(cf => cf.ByAutomationId("131")).AsCalendar();26            calendar.SetSelectedDate(DateTime.Now);27            Console.ReadLine();28        }29    }30}SetSelectedDate
Using AI Code Generation
1using FlaUI.Core;2using FlaUI.Core.AutomationElements;3using FlaUI.Core.AutomationElements.Infrastructure;4using FlaUI.Core.Definitions;5using FlaUI.Core.Tools;6using System;7using System.Collections.Generic;8using System.Linq;9using System.Text;10using System.Threading.Tasks;11{12    {13        static void Main(string[] args)14        {15            var app = FlaUI.Core.Application.Launch(@"C:\Users\Public\Documents\Syncfusion\Test\WinForms\SetSelectedDate
Using AI Code Generation
1var app = FlaUI.Core.Application.Launch(@"C:\Windows\System32\calc.exe");2var automation = FlaUI.Core.Automation.AutomationFactory.GetAutomation();3var window = app.GetMainWindow(automation);4var calendar = window.FindFirstDescendant(cf => cf.ByControlType(FlaUI.Core.Definitions.ControlType.Calendar)).AsCalendar();5calendar.SetSelectedDate(new System.DateTime(2019, 1, 1));6var app = FlaUI.Core.Application.Launch(@"C:\Windows\System32\calc.exe");7var automation = FlaUI.Core.Automation.AutomationFactory.GetAutomation();8var window = app.GetMainWindow(automation);9var calendar = window.FindFirstDescendant(cf => cf.ByControlType(FlaUI.Core.Definitions.ControlType.Calendar)).AsCalendar();10calendar.SetSelectedDate(new System.DateTime(2019, 1, 1));11var app = FlaUI.Core.Application.Launch(@"C:\Windows\System32\calc.exe");12var automation = FlaUI.Core.Automation.AutomationFactory.GetAutomation();13var window = app.GetMainWindow(automation);14var calendar = window.FindFirstDescendant(cf => cf.ByControlType(FlaUI.Core.Definitions.ControlType.Calendar)).AsCalendar();15calendar.SetSelectedDate(new System.DateTime(2019, 1, 1));16var app = FlaUI.Core.Application.Launch(@"C:\Windows\System32\calc.exe");17var automation = FlaUI.Core.Automation.AutomationFactory.GetAutomation();18var window = app.GetMainWindow(automation);19var calendar = window.FindFirstDescendant(cf => cf.ByControlType(FlaUI.Core.Definitions.ControlType.Calendar)).AsCalendar();20calendar.SetSelectedDate(new System.DateTime(2019, 1, 1));Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
