manggaraaaa
manggaraaaa

Reputation: 820

How i change custom input in TextEdit Devexpress

I have the TextEdit with properties max value = 6, the default value is "000000" and i will replace the value according to user input. For example when user input "69" in TextEdit, the final value TextEdit to be "000069". How i prepare that using c# ?

Please help me to prepare that...

Upvotes: 0

Views: 2761

Answers (2)

Stefano
Stefano

Reputation: 26

Try this (add EditValueChanging event handler to your text edit):

    private void textEdit_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
    {
        const int MaxLength = 6;
        var editor = (DevExpress.XtraEditors.TextEdit)sender;

        if (e.NewValue != null)
        {
            var s = (string)e.NewValue;
            s = s.TrimStart('0');

            if (string.IsNullOrWhiteSpace(s) == false)
            {
                BeginInvoke(new MethodInvoker(delegate
                {
                    editor.Text = s.Substring(0, Math.Min(s.Length, MaxLength)).PadLeft(MaxLength, '0');
                    editor.SelectionStart = MaxLength;
                }));
            }
        }
    }

Upvotes: 1

Niranjan Singh
Niranjan Singh

Reputation: 18290

Use the Edit Mask on TextEdit control. To achieve required functionality you can set the TextEdit.Properties.Mask.MaskType property to to Simple and set the TextEdit.Properties.Mask.EditMask property to "000000".

Go through documentation - Mask Editors Overview

To enable the Simple masked mode set the MaskProperties.MaskType property of the RepositoryItemTextEdit.Mask object to MaskType.Simple. The mask itself should be specified via the MaskProperties.EditMask property.

Example:

textEdit1.Properties.Mask.EditMask = "000000";
textEdit1.Properties.Mask.UseMaskAsDisplayFormat = true;
textEdit1.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Simple;

If you do not want to mask the editor control then go for Formatting, Here is an example:
How to: Add Custom Text to a Formatted String

textEdit1.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
textEdit1.Properties.DisplayFormat.FormatString = "{0:d6}";

Hope this help

Upvotes: 1

Related Questions