Numeric Input Control?

1 Answer 310 Views
General Discussions MaskedInput (Numeric, DateTime, Text, Currency) NumericUpDown
Bradley
Top achievements
Rank 2
Iron
Iron
Iron
Bradley asked on 31 Oct 2024, 05:31 PM

Is there a control in WPF that I can use for numeric input that looks just like a textbox, but doesn't allow anything non-numeric?  I've looked at "Masked Input", and don't like the look nor the behavior (e.g. I don't like to see the template, and putting the curser at the beginning of field and hitting a number puts the number in the first digit and fills the rest with zeroes - nothing like a TextBox).  I've also looked at the "Numeric UpDown", but haven't found a simple example that looks like a TextBox - all the examples show with the up and down arrows, which I don't want.  I'm sure it's just a matter of formatting/templating/whatever, but I'm not a UI guy - I just want to drop the control on my form and maybe tweak a few parameters.

I would just use a TextBox bound to an integer, but the problem with that is if the user enters something non-numeric, a conversion exception is thrown, rather than a validation error.  At that point the form validates just fine because the backing field hasn't actually changed yet, so the user can, for example, hit [Save] while there are errors shown on the screen, and the [Save] logic thinks all is well.

Any ideas?

Thanks!

Brad.

1 Answer, 1 is accepted

Sort by
0
Stenly
Telerik team
answered on 05 Nov 2024, 07:45 AM

Hello Brad,

For this requirement, you can use the RadNumericUpDown control and set its ShowButtons property to false, which will hide the arrow buttons for incrementing/decrementing the value. 

<telerik:RadNumericUpDown ShowButtons="False"/>

The produced result is as follows:

If you would like to format the value, you could extend the RadNumericUpDown control and override the FormatEdit and FormatDisplay methods to return the value formatted as needed:

public class CustomNumericUpDown : RadNumericUpDown
{
    public override string FormatDisplay()
    {
        return this.Value.ToString();
    }

    public override string FormatEdit()
    {
        return this.Value.ToString();
    }
}

The produced result after applying this suggestion is as follows:

I hope the provided information will be of help to you.

Regards,
Stenly
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
General Discussions MaskedInput (Numeric, DateTime, Text, Currency) NumericUpDown
Asked by
Bradley
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Stenly
Telerik team
Share this question
or