Bart Gijssens
Bart Gijssens

Reputation: 1612

What is the best way to format an edit box that represents a numeric value?

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:

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.

Upvotes: 1

Views: 2226

Answers (3)

Willem
Willem

Reputation: 9486

You need to set the UseMaskAsDisplayFormat = true:

Have a look here and here

EDIT:

Nice example here

Upvotes: 4

user586399
user586399

Reputation:

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.

Upvotes: 0

Sun Flower
Sun Flower

Reputation: 19

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

Upvotes: 0

Related Questions