Prevent user from typing beyond maximum value

5 posts, 0 answers
  1. Mark
    Mark avatar
    24 posts
    Member since:
    Jul 2012

    Posted 05 Nov 2012 Link to this post

    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?
  2. Vladi
    Admin
    Vladi avatar
    744 posts

    Posted 08 Nov 2012 Link to this post

    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.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Mark
    Mark avatar
    24 posts
    Member since:
    Jul 2012

    Posted 08 Nov 2012 Link to this post

    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. :)
  5. Vladi
    Admin
    Vladi avatar
    744 posts

    Posted 12 Nov 2012 Link to this post

    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.

  6. Haegen
    Haegen avatar
    1 posts
    Member since:
    Oct 2012

    Posted 14 Nov 2012 Link to this post

    Thanks. I'll give that a try.
Back to Top
UI for WPF is Visual Studio 2017 Ready