NumericUpDown: Shouldn't there be a single event for when the user made a change that should be acted upon?

Open
#1,750 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
desktop

Research direction

Start by tracing NumericUpDown's ValueChanged handling, UpdateValueOnEnterKey behavior, and the spinner-related events described in the issue. Clarify the desired event semantics for Enter/Return, focus loss, and completed spinner changes; done means an agreed API that avoids per-keystroke actions while reporting the applied value.

Written by the indexing model from the issue text.

Description

When the user is editing a NumericUpDown, every keystroke raises a ValueChanged event. This is not ideal when it's linked to something that you don't want changed more frequently than necessary. In the past I have dealt with this by handling the KeyUp, LostFocus, and Spinned events like this:

private void TheIntegerUpDown_KeyUp(object sender, KeyEventArgs e)
{
	if (e.Key == Key.Enter || e.Key == Key.Return)
		if (TheIntegerUpDown.Value.HasValue)
			Update(TheIntegerUpDown.Value);
}

private void TheIntegerUpDown_LostFocus(object sender, RoutedEventArgs e)
{
	if (TheIntegerUpDown.Value.HasValue)
		Update(TheIntegerUpDown.Value);
}

private void TheIntegerUpDown_Spinned(object sender, Xceed.Wpf.Toolkit.SpinEventArgs e)
{
	if (TheIntegerUpDown.Value.HasValue && TheIntegerUpDown.Increment.HasValue)
	{
		if (e.Direction == Xceed.Wpf.Toolkit.SpinDirection.Increase)
			Update(TheIntegerUpDown.Value + TheIntegerUpDown.Increment);
		else if (e.Direction == Xceed.Wpf.Toolkit.SpinDirection.Decrease)
			Update(TheIntegerUpDown.Value - TheIntegerUpDown.Increment);
	}
}

This seem like a lot of code to handle something conceptually very simple. I see there is an UpdateValueOnEnterKey property which could simplify this a bit, but when that's set to true, using the up/down spinner buttons no longer raises the ValueChanged event. Furthermore, let's say you needed to look at multiple NumericUpDowns whenever a spinner button on one is used. You would need some mechanism to indicate one particular NumericUpDown's new value, while the others are unchanged.

I can envision a few ways of solving this, if an elegant solution does not exist already:

  • An ActionableValueChanged event (couldn't come up with a better name) that's only raised when the user is typing and hits enter/return, the NumericUpDown loses focus, or a spinner button is pressed.
  • A SpinCompleted event so one can easily get the NumericUpDown's Value after the change has been applied.
Dominant language
C#
Stars
4.2k
Forks
912
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from xceedsoftware/wpftoolkit

All issues in xceedsoftware/wpftoolkit

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.