Best FlaUI code snippet using FlaUI.Core.AutomationElements.Calendar.AddToSelection
Calendar.cs
Source:Calendar.cs  
...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                }167168                // set year169                AutomationElement headerBtn = FindFirstChild(cf => cf.ByAutomationId("PART_HeaderButton"));170                string headerName = headerBtn.Name;171                string[] parts = headerName.Split('-');172                int yearLow = Convert.ToInt32(parts[0]);173                int yearHigh = Convert.ToInt32(parts[1]);174175                if (date.Year < yearLow)176                {177                    AutomationElement prevBtn = FindFirstChild(cf => cf.ByControlType(ControlType.Button));178                    while (date.Year < yearLow)179                    {180                        if (prevBtn.Patterns.Invoke.TryGetPattern(out var invokePattern))181                        {182                            invokePattern.Invoke(); // go to previous decade183                            headerName = headerBtn.Name;184                            parts = headerName.Split('-');185                            yearLow = Convert.ToInt32(parts[0]);186                        }187                        else188                        {189                            break; // invoke pattern not supported on previous decade button190                        }191                    }192                }193                else if (date.Year > yearHigh)194                {195                    AutomationElement nextBtn = FindFirstChild(cf => cf.ByAutomationId("PART_NextButton"));196                    while (date.Year > yearHigh)197                    {198                        if (nextBtn.Patterns.Invoke.TryGetPattern(out var invokePattern))199                        {200                            invokePattern.Invoke(); // go to next decade201                            headerName = headerBtn.Name;202                            parts = headerName.Split('-');203                            yearHigh = Convert.ToInt32(parts[1]);204                        }205                        else206                        {207                            break; // invoke pattern not supported on next decade button208                        }209                    }210                }211212                AutomationElement[] buttons = FindAllChildren(cf => cf.ByControlType(ControlType.Button));213                for (int i = 3; i < buttons.Length; i++) // iterate through all the year buttons214                {215                    AutomationElement button = buttons[i];216                    if (button.Name == date.Year.ToString())217                    {218                        if (button.Patterns.Invoke.TryGetPattern(out var invokePattern))219                        {220                            invokePattern.Invoke(); // select year221                        }222                    }223                }224225                // set month226                AutomationElement[] monthButtons = FindAllChildren(cf => cf.ByControlType(ControlType.Button));227                for (int i = 3; i < monthButtons.Length; i++)228                {229                    AutomationElement monthBtn = monthButtons[i];230                    DateTime crtMonthDate = DateTime.Parse(monthBtn.Name, CultureInfo.CurrentCulture);231                    if (crtMonthDate.Month == date.Month)232                    {233                        if (monthBtn.Patterns.Invoke.TryGetPattern(out var invokePattern))234                        {235                            invokePattern.Invoke(); // select month236                        }237                    }238                }239240                // set day241                AutomationElement[] dayButtons = FindAllChildren(cf => cf.ByControlType(ControlType.Button));242                DateTime dateDayMonthYear = new DateTime(date.Year, date.Month, date.Day);243                for (int i = 3; i < dayButtons.Length; i++)244                {245                    AutomationElement dayBtn = dayButtons[i];246                    string dayStr = dayBtn.Name;247248                    DateTime currentDate;249                    try250                    {251                        currentDate = DateTime.Parse(dayStr, CultureInfo.CurrentCulture);252                    }253                    catch254                    {255                        continue;256                    }257258                    if (currentDate == dateDayMonthYear)259                    {260                        if (add == true)261                        {262                            if (dayBtn.Patterns.SelectionItem.TryGetPattern(out var selectionItemPattern))263                            {264                                selectionItemPattern.AddToSelection();265                            }266                        }267                        else268                        {269                            if (dayBtn.Patterns.Invoke.TryGetPattern(out var invokePattern))270                            {271                                invokePattern.Invoke();272                            }273                        }274                    }275                }276            }277            else278            {
...CalendarTests.cs
Source:CalendarTests.cs  
...33            Assert.That(selectedDates[0], Is.EqualTo(date));34        }3536        [Test]37        public void AddToSelectionTest()38        {39            DateTime date1 = new DateTime(2020, 5, 20); // 20-May-202040            calendar.SelectDate(date1);41            DateTime date2 = new DateTime(2020, 5, 23); // 23-May-202042            calendar.AddToSelection(date2);43            DateTime[] selectedDates = calendar.SelectedDates;44            Assert.That(selectedDates, Has.Length.EqualTo(2));45            Assert.That(selectedDates[0], Is.EqualTo(date1));46            Assert.That(selectedDates[1], Is.EqualTo(date2));47        }4849        [Test]50        public void SelectRangeTest()51        {52            DateTime date1 = new DateTime(2021, 3, 8); // 8-Mar-202153            DateTime date2 = new DateTime(2021, 3, 9); // 9-Mar-202154            DateTime date3 = new DateTime(2021, 3, 11); // 11-Mar-202155            DateTime[] dates = new DateTime[] { date1, date2, date3 };56            calendar.SelectRange(dates);
...CalendarAddToSelectionExecutor.cs
Source:CalendarAddToSelectionExecutor.cs  
2using System;3using FlaUI.Core.AutomationElements;4namespace FlaNium.Desktop.Driver.CommandExecutors.Elements.Calendar5{6    class CalendarAddToSelectionExecutor : CommandExecutorBase7    {8        #region Methods9        protected override string DoImpl()10        {11            var registeredKey = this.ExecutedCommand.Parameters["ID"].ToString();12            var value = this.ExecutedCommand.Parameters["dateTime"].ToString();13            var element = this.Automator.ElementsRegistry.GetRegisteredElement(registeredKey);14            var calendar = element.FlaUIElement.AsCalendar();15            DateTime date = DateTime.Parse(value);16            calendar.AddToSelection(date);17            return this.JsonResponse();18        }19        #endregion20    }21}...AddToSelection
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using FlaUI.Core;7using FlaUI.Core.AutomationElements;8using FlaUI.Core.AutomationElements.Infrastructure;9using FlaUI.Core.Definitions;10using FlaUI.Core.Tools;11using FlaUI.UIA3;12using System.Windows.Forms;13using FlaUI.Core.Input;14{15    {16        static void Main(string[] args)17        {18            var app = FlaUI.Core.Application.Launch("notepad.exe");19            var automation = new UIA3Automation();20            var window = app.GetMainWindow(automation);21            var textBox = window.FindFirstDescendant(cf => cf.ByName("Text Editor")).AsTextBox();22            textBox.Enter("Hello World");23            window.Close();24        }25    }26}27using System;28using System.Collections.Generic;29using System.Linq;30using System.Text;31using System.Threading.Tasks;32using FlaUI.Core;33using FlaUI.Core.AutomationElements;34using FlaUI.Core.AutomationElements.Infrastructure;35using FlaUI.Core.Definitions;36using FlaUI.Core.Tools;37using FlaUI.UIA3;38using System.Windows.Forms;39using FlaUI.Core.Input;40{41    {42        static void Main(string[] args)43        {44            var app = FlaUI.Core.Application.Launch("notepad.exe");45            var automation = new UIA3Automation();46            var window = app.GetMainWindow(automation);47            var textBox = window.FindFirstDescendant(cf => cf.ByName("Text Editor")).AsTextBox();48            textBox.Enter("Hello World");49            window.Close();50        }51    }52}53using System;54using System.Collections.Generic;55using System.Linq;56using System.Text;57using System.Threading.Tasks;58using FlaUI.Core;59using FlaUI.Core.AutomationElements;60using FlaUI.Core.AutomationElements.Infrastructure;61using FlaUI.Core.Definitions;62using FlaUI.Core.Tools;63using FlaUI.UIA3;64using System.Windows.Forms;65using FlaUI.Core.Input;66{67    {68        static void Main(string[] args)69        {70            var app = FlaUI.Core.Application.Launch("notepad.exe");AddToSelection
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;12using System.Windows.Forms;13using System.Drawing;14{15    {16        static void Main(string[] args)17        {18            var application = FlaUI.Core.Application.Launch(@"C:\Windows\System32\calc.exe");19            var automation = new UIA3Automation();20            var window = application.GetMainWindow(automation);21            var calendar = window.FindFirstDescendant(cf => cf.ByControlType(ControlType.Calendar)).AsCalendar();22            var currentSelection = calendar.GetSelection();23            var currentDate = currentSelection.FirstOrDefault();24            var nextDate = currentDate.AddDays(1);25            calendar.AddToSelection(nextDate);26            var newSelection = calendar.GetSelection();27            var newDate = newSelection.FirstOrDefault();28            if (newDate == nextDate)29            {30                Console.WriteLine("The new date is the next date");31            }32            {33                Console.WriteLine("The new date is not the next date");34            }35            Console.WriteLine("Press any key to exit");36            Console.ReadKey();37        }38    }39}AddToSelection
Using AI Code Generation
1using FlaUI.Core;2using FlaUI.Core.AutomationElements;3using FlaUI.Core.AutomationElements.Infrastructure;4using FlaUI.Core.Definitions;5using FlaUI.Core.Input;6using FlaUI.Core.Tools;7using FlaUI.UIA3;8using System;9using System.Collections.Generic;10using System.Drawing;11using System.Linq;12using System.Text;13using System.Threading;14using System.Threading.Tasks;15{16    {17        static void Main(string[] args)18        {19            var application = FlaUI.Core.Application.Launch(@"C:\Windows\System32\notepad.exe");20            Thread.Sleep(2000);21            var mainWindow = application.GetMainWindow(Automation);22            var childElement = mainWindow.FindFirstDescendant(x => x.ByControlType(ControlType.Edit));23            childElement.Enter("Hello World");24            application.Close();25        }26        private static UIA3Automation Automation = new UIA3Automation();27    }28}AddToSelection
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;8using FlaUI.Core.Definitions;AddToSelection
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;9using FlaUI.Core.Definitions;10using FlaUI.Core.Input;11using FlaUI.Core.Tools;12using FlaUI.Core.WindowsAPI;13using FlaUI.Core.WindowsAPI;AddToSelection
Using AI Code Generation
1using FlaUI.Core;2using FlaUI.Core.AutomationElements;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            using (var automation = new UIA3Automation())16            {17                var application = FlaUI.Core.Application.Launch(@"C:\Users\Administrator\Desktop\CalendarTest\CalendarTest\bin\Debug\CalendarTest.exe");18                var window = application.GetMainWindow(automation);19                var calendar = window.FindFirstDescendant(cf => cf.ByAutomationId("calendar1")).AsCalendar();20                var date = new DateTime(2018, 2, 27);21                calendar.AddToSelection(date);22            }23        }24    }25}AddToSelection
Using AI Code Generation
1using System;2using System.Windows.Automation;3using FlaUI.Core;4using FlaUI.Core.AutomationElements;5using FlaUI.Core.Definitions;6using FlaUI.Core.Input;7using FlaUI.Core.WindowsAPI;8using FlaUI.UIA3;9using FlaUI.Core.Tools;10using FlaUI.Core.Conditions;11using System.Drawing;12using System.Windows.Forms;13{14    {15        static void Main(string[] args)16        {17            var application = FlaUI.Core.Application.Launch(@"C:\Program Files (x86)\Windows Media Player\wmplayer.exe");18            var automation = new UIA3Automation();19            var window = application.GetMainWindow(automation);20            window.WaitWhileBusy();21            var calendar = window.FindFirstDescendant(cf => cf.ByControlType(ControlType.Calendar));22            calendar.AsCalendar().AddToSelection(new DateTime(2018, 4, 4));23            application.Close();24        }25    }26}27using System;28using System.Windows.Automation;29using FlaUI.Core;30using FlaUI.Core.AutomationElements;31using FlaUI.Core.Definitions;32using FlaUI.Core.Input;33using FlaUI.Core.WindowsAPI;34using FlaUI.UIA3;35using FlaUI.Core.Tools;36using FlaUI.Core.Conditions;37using System.Drawing;38using System.Windows.Forms;39{40    {41        static void Main(string[] args)42        {43            var application = FlaUI.Core.Application.Launch(@"C:\Program Files (x86)\Windows Media Player\wmplayer.exe");44            var automation = new UIA3Automation();45            var window = application.GetMainWindow(automation);46            window.WaitWhileBusy();47            var calendar = window.FindFirstDescendant(cf => cf.ByControlType(ControlType.Calendar));48            calendar.AsCalendar().AddToSelection(new DateTime(2018, 4, 4));49            application.Close();50        }51    }52}AddToSelection
Using AI Code Generation
1using System;2using System.Windows.Automation;3using FlaUI.Core;4using FlaUI.Core.AutomationElements;5using FlaUI.Core.Definitions;6using FlaUI.Core.Tools;7using FlaUI.UIA3;8using System.Threading;9using FlaUI.Core.Input;10using FlaUI.Core.WindowsAPI;11using System.Collections.Generic;12using System.Linq;13{14    {15        static void Main(string[] args)16        {17            var application = Application.Launch(@"C:\Windows\System32\calc.exe");18            var automation = new UIA3Automation();19            Thread.Sleep(1000);20            var window = application.GetMainWindow(automation);AddToSelection
Using AI Code Generation
1using System;2using FlaUI.Core;3using FlaUI.Core.AutomationElements;4using FlaUI.Core.Definitions;5using FlaUI.Core.Tools;6using FlaUI.UIA3;7using FlaUI.Core.Conditions;8{9    {10        static void Main(string[] args)11        {12            var application = Application.Launch(@"C:\Windows\System32\calc.exe");13            var automation = new UIA3Automation();14            var mainWindow = application.GetMainWindow(automation);15            var button = mainWindow.FindFirstDescendant(cf => cf.ByControlType(ControlType.Button).And(cf.ByName("Date Calculator"))).AsButton();16            button.Click();17            var calendar = mainWindow.FindFirstDescendant(cf => cf.ByControlType(ControlType.Calendar)).AsCalendar();18            var date = new DateTime(2020, 02, 01);19            calendar.SelectDate(date);20            calendar.AddToSelection(date.AddDays(5));21            calendar.AddToSelection(date.AddDays(10));22            calendar.AddToSelection(date.AddDays(15));23            calendar.AddToSelection(date.AddDays(20));24            calendar.AddToSelection(date.AddDays(25));25            calendar.AddToSelection(date.AddDays(30));26            calendar.AddToSelection(date.AddDays(35));27            calendar.AddToSelection(date.AddDays(40));28            calendar.AddToSelection(date.AddDays(45));29            calendar.AddToSelection(date.AddDays(50));30            calendar.AddToSelection(date.AddDays(55));31            calendar.AddToSelection(date.AddDays(60));32            calendar.AddToSelection(date.AddDays(65));33            calendar.AddToSelection(date.AddDays(70));34            calendar.AddToSelection(date.AddDays(75));35            calendar.AddToSelection(date.AddDays(80));36            calendar.AddToSelection(date.AddDays(85));37            calendar.AddToSelection(date.AddDays(90));38            calendar.AddToSelection(date.AddDays(95));39            calendar.AddToSelection(date.AddDays(100));40            calendar.AddToSelection(date.AddDays(105));41            calendar.AddToSelection(date.AddDays(110));42            calendar.AddToSelection(date.AddDays(115));43            calendar.AddToSelection(date.AddDays(120));44            calendar.AddToSelection(date.AddDays(125));45            calendar.AddToSelection(date.AddDays(130));46            calendar.AddToSelection(date.AddDays(135));47            calendar.AddToSelection(date.AddDays(140));48            calendar.AddToSelection(date.AddDays(145));49            calendar.AddToSelection(date.AddDays(150));50            calendar.AddToSelection(date.AddDays(155));51            calendar.AddToSelection(date.AddDays(160Learn 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!!
