Skip to content

Fix 7 critical bugs in NumericUpDown and DateTimePicker#4565

Open
Thuram2003 wants to merge 2 commits intoMahApps:developfrom
Thuram2003:fix/critical-bugs-numericupdown-datetimepicker
Open

Fix 7 critical bugs in NumericUpDown and DateTimePicker#4565
Thuram2003 wants to merge 2 commits intoMahApps:developfrom
Thuram2003:fix/critical-bugs-numericupdown-datetimepicker

Conversation

@Thuram2003
Copy link
Copy Markdown

This PR fixes multiple critical issues in NumericUpDown and DateTimePicker controls in MahApps.Metro, improving stability, usability, and performance.

🔧 Key Fixes
Fixed keyboard input blocking in NumericUpDown
Fixed inconsistent DateTimeKind in DateTimePicker
Prevented duplicate ValueChanged event firing
Replaced unsafe async void method to improve stability
Fixed potential integer overflow in hexadecimal formatting
Improved event handler cleanup to prevent memory leaks
Removed unnecessary double value coercion for better performance

🎯 Impact
Better user input experience (smoother typing)
More consistent and reliable date handling
Improved performance and memory safety
Reduced risk of crashes and unexpected behavior

🧪 Testing
Verified compilation across target frameworks (.NET Framework & .NET 6/8)
Manually tested all affected controls
No breaking changes introduced
📌 Notes
All changes are backward compatible and focused on improving internal control behavior without affecting public APIs.

This commit addresses 7 critical issues identified through comprehensive code analysis:

1. NumericUpDown keyboard input blocking (MahApps#4561)
   - Users can now type intermediate values (e.g., "5" when minimum is 50)
   - Coercion happens on focus lost instead of blocking input
   - Improves user experience significantly

2. DateTimePicker DateTimeKind inconsistency (MahApps#4551)
   - All DateTime values now consistently return DateTimeKind.Local
   - Eliminates timezone-related bugs and data inconsistencies
   - Uses DateTime.SpecifyKind() for consistency

3. Double ValueChanged event firing
   - Removed duplicate OnValueChanged() call in ChangeValueFromTextInput
   - Event now fires exactly once per value change
   - Improves performance and prevents duplicate processing

4. Dangerous async void method
   - Converted SimulateDecimalPointKeyPress from async void to synchronous
   - Eliminates risk of unhandled exceptions crashing the application
   - Removed unnecessary await Task.FromResult(true)

5. Integer overflow in hexadecimal formatting
   - Added validation before casting double to int in TryFormatHexadecimal
   - Prevents OverflowException when value > int.MaxValue
   - Properly rejects decimal values in hex mode

6. Event handler memory leaks in OnApplyTemplate
   - Added cleanup code to remove old handlers before adding new ones
   - Converted lambda expressions to named methods for proper removal
   - Prevents memory leaks when templates change (theme/style switches)

7. Unnecessary double coercion in SetValueTo
   - Removed manual CoerceValue() call
   - Dependency property system already handles coercion automatically
   - Improves performance by eliminating redundant processing

All fixes:
- Compile successfully across all target frameworks (net462, net6.0-windows, net8.0-windows)
- Include no breaking changes to the public API
- Are thoroughly documented with before/after code examples
- Have comprehensive testing guidelines

Fixes MahApps#4561, MahApps#4551
@Thuram2003
Copy link
Copy Markdown
Author

@dotnet-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant