Vinicius Gonçalves
Vinicius Gonçalves

Reputation: 2724

Mask for only negative numbers

How to build a mask for user entry only negative values in DevExpress/WinForms TextEdit component?

I am trying to do this, but with no success:

enter image description here

Same question here, but this solution is not working

I think it's a bug.

Upvotes: 1

Views: 2988

Answers (4)

onur
onur

Reputation: 374

Try this:

txtEdit.Properties.Mask.EditMask = "\\d-";

Which version of DX do you use ?

Upvotes: 0

Beldi Anouar
Beldi Anouar

Reputation: 2180

Try this solution :

In your form load :

TextEdit1.Properties.Mask.EditMask = "-#0.0000";
TextEdit1.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;
TextEdit1.Properties.Mask.UseMaskAsDisplayFormat = false;
TextEdit1.Properties.EditFormat.FormatString = "-#0.0000";

And handel the event "CustomDisplayText" of your textEdit :

private void TextEdit1_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
{
          if ((e.Value != null) && !e.Value.Equals("")) 
           {
               e.DisplayText = Convert.ToDouble(e.Value).ToString("-#0.0000");
           }       
}

Upvotes: 0

Gosha_Fighten
Gosha_Fighten

Reputation: 3858

If you work with numbers, I suggest you use SpinEdit. To limit it to accept only negative numbers, use the RepositoryItemSpinEdit.MaxValue and RepositoryItemSpinEdit.MinValue properties.

spinEdit1.Properties.MaxValue = -1;
spinEdit1.Properties.MinValue = decimal.MinValue;

If you need TextEdit, I suggest you use Abdellah's mask. So that you get TextEdit.EditValue as a number instead of a string, use the ParseEditValue event.

textEdit1.Properties.Mask.EditMask = "-[0-9]*[.]{0,1}[0-9]*";
textEdit1.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx;

private void textEdit1_ParseEditValue(object sender, DevExpress.XtraEditors.Controls.ConvertEditValueEventArgs e) {
    if (e.Value is string) {
        e.Value = double.Parse(e.Value.ToString());
        e.Handled = true;
    }
}

Upvotes: 1

Abdellah OUMGHAR
Abdellah OUMGHAR

Reputation: 3745

You can use RegEx mask type : -[0-9]*[.]{0,1}[0-9-]*

enter image description here

or you can handle EditValueChanging event like this :

private void textEdit1_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
{
    e.Cancel = !e.NewValue.ToString().Contains("-");
}

Upvotes: 1

Related Questions