简体   繁体   中英

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...

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

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;
                }));
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM