@@ -57,6 +57,11 @@ public static MessageBoxResult Show(
5757 _ = SetImageOfMessageBoxAsync ( icon ) ;
5858 }
5959 SetButtonVisibilityFocusAndResult ( button , defaultResult ) ;
60+
61+ // This ensures that if the message box is closed without using the answer buttons
62+ // It will still have a meaningful result based on its type
63+ _result = msgBox . DefaultCloseResult ;
64+
6065 msgBox . ShowDialog ( ) ;
6166 return _result ;
6267 }
@@ -163,15 +168,30 @@ private async Task SetImageAsync(string imageName)
163168 Img . Source = imageSource ;
164169 }
165170
171+ // What the result should be if the message box is closed outside of the direct response buttons - e.g title bar close button
172+ // Mostly replicates System.Windows.MessageBox behavior
173+ // https://learn.microsoft.com/en-us/dotnet/api/system.windows.messageboxresult#remarks
174+ private MessageBoxResult DefaultCloseResult => _button switch
175+ {
176+ MessageBoxButton . OK => MessageBoxResult . OK ,
177+ MessageBoxButton . OKCancel => MessageBoxResult . Cancel ,
178+ MessageBoxButton . YesNoCancel => MessageBoxResult . Cancel ,
179+
180+ // For YesNo this should only be used in a forced close edge case (e.g. alt f4)
181+ // Most callers make the mistake of checking for No instead of not Yes - so best not to return None etc
182+ MessageBoxButton . YesNo => MessageBoxResult . No ,
183+
184+ // covers unsupported types, e.g. AbortRetryIgnore
185+ _ => MessageBoxResult . None
186+ } ;
187+
188+
166189 private void KeyEsc_OnPress ( object sender , ExecutedRoutedEventArgs e )
167190 {
168191 if ( _button == MessageBoxButton . YesNo )
169192 // Follow System.Windows.MessageBox behavior
170193 return ;
171- else if ( _button == MessageBoxButton . OK )
172- _result = MessageBoxResult . OK ;
173- else
174- _result = MessageBoxResult . Cancel ;
194+
175195 DialogResult = false ;
176196 Close ( ) ;
177197 }
@@ -196,23 +216,11 @@ private void Button_Cancel(object sender, RoutedEventArgs e)
196216 {
197217 e . Handled = true ;
198218
199- // Replicates System.Windows.MessageBox behavior
200- // https://learn.microsoft.com/en-us/dotnet/api/system.windows.messageboxresult#remarks
201-
202- switch ( _button )
219+ if ( _button == MessageBoxButton . YesNo )
203220 {
204221 // For YesNo, the close button should be hidden and inaccessible.
205- case MessageBoxButton . YesNo :
206- App . API . LogWarn ( ClassName , "Close button was invoked despite being hidden for YesNo dialog" ) ;
207- return ;
208- case MessageBoxButton . OK :
209- _result = MessageBoxResult . OK ;
210- break ;
211- case MessageBoxButton . OKCancel :
212- case MessageBoxButton . YesNoCancel :
213- default :
214- _result = MessageBoxResult . Cancel ;
215- break ;
222+ App . API . LogWarn ( ClassName , "Close button was invoked despite being hidden for YesNo dialog" ) ;
223+ return ;
216224 }
217225
218226 msgBox . Close ( ) ;
0 commit comments