This is a migrated thread and some comments may be shown as answers.

Prevent user from typing beyond maximum value

4 Answers 473 Views
NumericUpDown
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 05 Nov 2012, 07:50 AM
Hi, is there any workaround on preventing the user from  typing beyond the maximum value? 

Please help we are still evaluating a trial copy and this demo will help my boss to decide if she will buy telerik or not.(I know this is not necessary, but lately no one has been replying on questions I post here LOL)

So please... help?

4 Answers, 1 is accepted

Sort by
0
Vladi
Telerik team
answered on 08 Nov 2012, 10:08 AM
Hi Mark,

Thank you for contacting us.

By default If you set the Maximum property of the control when an user inputs a value grater than that Maximum when the control loses the focus the selected value automatically is set to the Maximum value. The same is true for the Minimum property of the control.

If this is not the behavior you want to achieve could you explain in more details what exactly do you want to achieve.

Hope this helps.

All the best,
Vladi
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Mark
Top achievements
Rank 1
answered on 09 Nov 2012, 12:52 AM
Hi,

That is not the behaviour I want. What I want is, the user should not be able to type an invalid value so validation is during typing, not when the control losses focus.

I have a NumericUpDown bound to an int property on my view model. In the property's setter, I have a code that check if the new value is within the range required. For example, my range is 1 - 100, if the user starts to type 101 the setter ignores the last input and returns the previous accepted value. In other words, if my NumericUpDown currently displays 10 and the user presses 1, nothing should happen, but if the user presses 0 which makes the current value 100, this will be accepted. Below is my code for the property.

private Int32 maximumLogEntries;
    private string MaximumLogEntriesPropname = EventLogDataFieldHelper.MaximumLogEntries;     
    public Int32 MaximumLogEntries
    {
        get
        {
            return maximumLogEntries;
        }
 
        set
        {
            if (maximumLogEntries == value)
            {
                return;
            }
            var oldValue = maximumLogEntries;
 
            dynamic resultMax;
            dynamic resultMin;
            GetMax(MaximumLogEntriesPropname, out resultMax);
            GetMin(MaximumLogEntriesPropname, out resultMin);
 
            dynamic outValue;
            ValidateViewModel.Validate(value, resultMin, resultMax, maximumLogEntries, out outValue);
            maximumLogEntries = outValue;
 
            // Update bindings, no broadcast
            RaisePropertyChanged(MaximumLogEntriesPropname);
        }
 
public static void Validate(dynamic newValue, dynamic min, dynamic max, dynamic oldValue, out dynamic outValue)
    {
        
        if (newValue < min || newValue > max)
            outValue = oldValue;
        else
            outValue = newValue;
       
    }  
Below is my Xaml:
<telerik:RadNumericUpDown Height="17.75" IsInteger="True" Width="150" UpdateValueEvent="PropertyChanged" Value="{Binding MaximumLogEntries, Converter={StaticResource IntToDoubleConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

Thanks for your reply. :)
0
Vladi
Telerik team
answered on 12 Nov 2012, 12:21 PM
Hi Mark,

The scenario you are trying to implement is not supported out of the box in the current version of RadNumericUpDown control.

It should be possible to implement the wanted behavior in the control with some custom code. As a suggestion I would suggest you to use the PreviewKeyDown event of the control and make adjustments to the value according to the current Value of the control.

Hope this helps.

Regards,
Vladi
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Haegen
Top achievements
Rank 1
answered on 15 Nov 2012, 12:47 AM
Thanks. I'll give that a try.
Tags
NumericUpDown
Asked by
Mark
Top achievements
Rank 1
Answers by
Vladi
Telerik team
Mark
Top achievements
Rank 1
Haegen
Top achievements
Rank 1
Share this question
or