@@ -27,7 +27,7 @@ internal class MonthCalendarAccessibleObject : ControlAccessibleObject
2727 public MonthCalendarAccessibleObject ( Control owner )
2828 : base ( owner )
2929 {
30- _owner = owner as MonthCalendar ;
30+ _owner = ( MonthCalendar ) owner ;
3131 }
3232
3333 public UiaCore . UIA ControlType =>
@@ -83,10 +83,6 @@ public override string Name
8383 }
8484
8585 name = string . Empty ;
86- if ( _owner == null )
87- {
88- return name ;
89- }
9086
9187 if ( _owner . _mcCurView == MCMV . MONTH )
9288 {
@@ -134,47 +130,34 @@ public override string Value
134130 {
135131 get
136132 {
137- var value = string . Empty ;
138- if ( _owner == null )
139- {
140- return value ;
141- }
142-
143133 try
144134 {
145135 if ( _owner . _mcCurView == MCMV . MONTH )
146136 {
147137 if ( System . DateTime . Equals ( _owner . SelectionStart . Date , _owner . SelectionEnd . Date ) )
148138 {
149- value = _owner . SelectionStart . ToLongDateString ( ) ;
150- }
151- else
152- {
153- value = string . Format ( "{0} - {1}" , _owner . SelectionStart . ToLongDateString ( ) , _owner . SelectionEnd . ToLongDateString ( ) ) ;
139+ return _owner . SelectionStart . ToLongDateString ( ) ;
154140 }
141+
142+ return string . Format ( "{0} - {1}" , _owner . SelectionStart . ToLongDateString ( ) , _owner . SelectionEnd . ToLongDateString ( ) ) ;
155143 }
156- else if ( _owner . _mcCurView == MCMV . YEAR )
144+
145+ if ( _owner . _mcCurView == MCMV . YEAR )
157146 {
158147 if ( System . DateTime . Equals ( _owner . SelectionStart . Month , _owner . SelectionEnd . Month ) )
159148 {
160- value = _owner . SelectionStart . ToString ( "y" ) ;
161- }
162- else
163- {
164- value = string . Format ( "{0} - {1}" , _owner . SelectionStart . ToString ( "y" ) , _owner . SelectionEnd . ToString ( "y" ) ) ;
149+ return _owner . SelectionStart . ToString ( "y" ) ;
165150 }
151+
152+ return string . Format ( "{0} - {1}" , _owner . SelectionStart . ToString ( "y" ) , _owner . SelectionEnd . ToString ( "y" ) ) ;
166153 }
167- else
168- {
169- value = string . Format ( "{0} - {1}" , _owner . SelectionRange . Start . ToString ( ) , _owner . SelectionRange . End . ToString ( ) ) ;
170- }
154+
155+ return string . Format ( "{0} - {1}" , _owner . SelectionRange . Start . ToString ( ) , _owner . SelectionRange . End . ToString ( ) ) ;
171156 }
172157 catch
173158 {
174- value = base . Value ;
159+ return base . Value ;
175160 }
176-
177- return value ;
178161 }
179162 set => base . Value = value ;
180163 }
@@ -195,6 +178,7 @@ internal override int ColumnCount
195178
196179 int columnCount = 0 ;
197180 bool success = true ;
181+
198182 while ( success )
199183 {
200184 success = GetCalendarGridInfo (
@@ -236,6 +220,7 @@ internal override int RowCount
236220
237221 int rowCount = 0 ;
238222 bool success = true ;
223+
239224 while ( success )
240225 {
241226 success = GetCalendarGridInfo (
@@ -271,6 +256,7 @@ internal override UiaCore.IRawElementProviderFragment ElementProviderFromPoint(d
271256 int innerY = ( int ) y ;
272257
273258 MCHITTESTINFO hitTestInfo = GetHitTestInfo ( innerX , innerY ) ;
259+
274260 switch ( ( MCHT ) hitTestInfo . uHit )
275261 {
276262 case MCHT . TITLEBTNPREV :
@@ -289,7 +275,7 @@ internal override UiaCore.IRawElementProviderFragment ElementProviderFromPoint(d
289275 case MCHT . CALENDARDATE :
290276 // Get calendar body's child.
291277 CalendarBodyAccessibleObject calendarBodyAccessibleObject = ( CalendarBodyAccessibleObject ) GetCalendarChildAccessibleObject ( _calendarIndex , CalendarChildType . CalendarBody ) ;
292- return calendarBodyAccessibleObject . GetFromPoint ( hitTestInfo ) ;
278+ return calendarBodyAccessibleObject ? . GetFromPoint ( hitTestInfo ) ;
293279
294280 case MCHT . TODAYLINK :
295281 return GetCalendarChildAccessibleObject ( _calendarIndex , CalendarChildType . TodayLink ) ;
@@ -361,7 +347,8 @@ public string GetCalendarChildName(int calendarIndex, CalendarChildType calendar
361347
362348 private CalendarCellAccessibleObject GetCalendarCell ( int calendarIndex , AccessibleObject parentAccessibleObject , int columnIndex )
363349 {
364- if ( columnIndex < 0 ||
350+ if ( parentAccessibleObject == null ||
351+ columnIndex < 0 ||
365352 columnIndex >= MAX_DAYS ||
366353 columnIndex >= ColumnCount )
367354 {
@@ -379,12 +366,16 @@ private CalendarCellAccessibleObject GetCalendarCell(int calendarIndex, Accessib
379366 out Kernel32 . SYSTEMTIME systemEndDate ,
380367 out Kernel32 . SYSTEMTIME systemStartDate ) ;
381368
382- DateTime endDate = DateTimePicker . SysTimeToDateTime ( systemEndDate ) . Date ;
383- DateTime startDate = DateTimePicker . SysTimeToDateTime ( systemStartDate ) . Date ;
384-
385369 if ( getNameResult && ! string . IsNullOrEmpty ( text ) )
386370 {
387- string cellName = GetCalendarCellName ( endDate , startDate , text , rowIndex == - 1 ) ;
371+ string cellName = string . Empty ;
372+
373+ if ( getDateResult )
374+ {
375+ DateTime endDate = DateTimePicker . SysTimeToDateTime ( systemEndDate ) . Date ;
376+ DateTime startDate = DateTimePicker . SysTimeToDateTime ( systemStartDate ) . Date ;
377+ cellName = GetCalendarCellName ( endDate , startDate , text , rowIndex == - 1 ) ;
378+ }
388379
389380 // The cell is present on the calendar, so create accessible object for it.
390381 return new CalendarCellAccessibleObject ( this , calendarIndex , parentAccessibleObject , rowIndex , columnIndex , cellName ) ;
@@ -414,7 +405,8 @@ private string GetCalendarCellName(DateTime endDate, DateTime startDate, string
414405
415406 private CalendarRowAccessibleObject GetCalendarRow ( int calendarIndex , AccessibleObject parentAccessibleObject , int rowIndex )
416407 {
417- if ( ( HasHeaderRow ? rowIndex < - 1 : rowIndex < 0 ) ||
408+ if ( parentAccessibleObject == null ||
409+ ( HasHeaderRow ? rowIndex < - 1 : rowIndex < 0 ) ||
418410 rowIndex >= RowCount )
419411 {
420412 return null ;
@@ -439,7 +431,7 @@ private CalendarRowAccessibleObject GetCalendarRow(int calendarIndex, Accessible
439431
440432 SelectionRange cellsRange = _owner . GetDisplayRange ( false ) ;
441433
442- if ( cellsRange . Start > DateTimePicker . SysTimeToDateTime ( endDate ) || cellsRange . End < DateTimePicker . SysTimeToDateTime ( startDate ) )
434+ if ( cellsRange == null || cellsRange . Start > DateTimePicker . SysTimeToDateTime ( endDate ) || cellsRange . End < DateTimePicker . SysTimeToDateTime ( startDate ) )
443435 {
444436 // Do not create row if the row's first cell is out of the current calendar's view range.
445437 return null ;
@@ -648,6 +640,7 @@ private unsafe void SendMouseInput(int x, int y, User32.MOUSEEVENTF flags)
648640 public void RaiseAutomationEventForChild ( UiaCore . UIA automationEventId , DateTime selectionStart , DateTime selectionEnd )
649641 {
650642 AccessibleObject calendarChildAccessibleObject = GetCalendarChildAccessibleObject ( selectionStart , selectionEnd ) ;
643+
651644 if ( calendarChildAccessibleObject != null )
652645 {
653646 calendarChildAccessibleObject . RaiseAutomationEvent ( automationEventId ) ;
@@ -661,13 +654,23 @@ public void RaiseAutomationEventForChild(UiaCore.UIA automationEventId, DateTime
661654
662655 private AccessibleObject GetCalendarChildAccessibleObject ( DateTime selectionStart , DateTime selectionEnd )
663656 {
664- int columnCount = ColumnCount ;
665-
666657 AccessibleObject bodyAccessibleObject = GetCalendarChildAccessibleObject ( _calendarIndex , CalendarChildType . CalendarBody ) ;
658+
659+ if ( bodyAccessibleObject == null )
660+ {
661+ return null ;
662+ }
663+
667664 for ( int row = 0 ; row < RowCount ; row ++ )
668665 {
669666 AccessibleObject rowAccessibleObject = GetCalendarChildAccessibleObject ( _calendarIndex , CalendarChildType . CalendarRow , bodyAccessibleObject , row ) ;
670- for ( int column = 0 ; column < columnCount ; column ++ )
667+
668+ if ( rowAccessibleObject == null )
669+ {
670+ continue ;
671+ }
672+
673+ for ( int column = 0 ; column < ColumnCount ; column ++ )
671674 {
672675 bool success = GetCalendarGridInfo (
673676 MCGIF . DATE ,
@@ -685,6 +688,7 @@ private AccessibleObject GetCalendarChildAccessibleObject(DateTime selectionStar
685688 }
686689
687690 AccessibleObject cellAccessibleObject = GetCalendarChildAccessibleObject ( _calendarIndex , CalendarChildType . CalendarCell , rowAccessibleObject , column ) ;
691+
688692 if ( cellAccessibleObject == null )
689693 {
690694 continue ;
@@ -715,7 +719,15 @@ internal override UiaCore.IRawElementProviderSimple[] GetColumnHeaderItems()
715719
716720 UiaCore . IRawElementProviderSimple [ ] headers =
717721 new UiaCore . IRawElementProviderSimple [ MonthCalendarAccessibleObject . MAX_DAYS ] ;
718- AccessibleObject headerRowAccessibleObject = GetCalendarChildAccessibleObject ( _calendarIndex , CalendarChildType . CalendarRow , this , - 1 ) ;
722+
723+ AccessibleObject bodyAccessibleObject = GetCalendarChildAccessibleObject ( _calendarIndex , CalendarChildType . CalendarBody , this , - 1 ) ;
724+ AccessibleObject headerRowAccessibleObject = GetCalendarChildAccessibleObject ( _calendarIndex , CalendarChildType . CalendarRow , bodyAccessibleObject , - 1 ) ;
725+
726+ if ( headerRowAccessibleObject == null )
727+ {
728+ return null ;
729+ }
730+
719731 for ( int columnIndex = 0 ; columnIndex < MonthCalendarAccessibleObject . MAX_DAYS ; columnIndex ++ )
720732 {
721733 headers [ columnIndex ] = GetCalendarChildAccessibleObject ( _calendarIndex , CalendarChildType . CalendarCell , headerRowAccessibleObject , columnIndex ) ;
0 commit comments