1

In my c# program I have a very simple DevExpress edit box that represents a numeric value.

enter image description here

What I would like to do is to add a restriction on the number of decimals in such a way that:

  • Users cannot type, paste or in any other way enter a value that contains more than a predefined number of decimals. I fact I just want to editbox to ignore the user's typing as soon as 3 decimals have been entered.
  • If a programmer sets the edit box' text, the value is rounded so that it meets the requirements.

What is the best way to do this?

Ps.: I thought this would solve my problem:

valueTextEdit.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
valueTextEdit.Properties.DisplayFormat.FormatString = "#.000;[#.000];0.000";

But it doesn't seem to do anything. I can still enter values with 10 decimals. Also in code I can set the edit box text to a value with a larger number of decimals.

Bart Gijssens
  • 1,572
  • 5
  • 20
  • 38

3 Answers3

4

You need to set the UseMaskAsDisplayFormat = true:

Have a look here and here

EDIT:

Nice example here

Willem
  • 9,166
  • 17
  • 68
  • 92
0

try this MaskTextBox control of .Net

you can use MaskedTextBox for entering data in numeric format in text box.

i hope this will help you

thank you

Sun Flower
  • 19
  • 4
  • 1
    The MaskTextBox is interesting, I did not know this. However I cannot use a non-DevExpress control because I use the DevExpress validation thing to validate the edit box value (e.g. for validating that it lies within a certain range). – Bart Gijssens Sep 08 '11 at 13:22
0

You can build your own class that derives (inherits) from TextBox, then override property Text to add your requirments:

internal class SmartTextBox : TextBox
{
    public SmartTextBox()
    {
    }

    public override string Text
    {
        get
        {
            return base.Text;
        }
        set
        {
            // validate 'value' before setting it
            base.Text = value;
        }
    }
}

After you build your project, you will find your new Control called SmartTextBox with .NET Controls.

EDIT: However, if you use TextBox for numeric input only, why don't you use NumericUpDown Control? It is much better, users can not input characters and you can even set the precision of decimal number.