开发者

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.

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

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 = DevEx开发者_JS百科press.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.


You need to set the UseMaskAsDisplayFormat = true:

Have a look here and here

EDIT:

Nice example here


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜