我如何在 TextEdit Devexpress 中更改自定义输入

How i change custom input in TextEdit Devexpress

我有属性最大值 = 6 的 TextEdit,默认值为“000000” 我将根据用户输入替换值。例如,当用户在 TextEdit 中输入“69”时,TextEdit 的最终值为“000069”。我如何使用 C# 准备它?

请帮我准备一下...

在 TextEdit 控件上使用编辑蒙版。要实现所需的功能,您可以将 TextEdit.Properties.Mask.MaskType property to to Simple and set the TextEdit.Properties.Mask.EditMask 属性 设置为“000000”。

查看文档 - 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.

示例:

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

如果您不想屏蔽编辑器控件,请选择 Formatting,这是一个示例:
How to: Add Custom Text to a Formatted String

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

希望对您有所帮助

试试这个(将 EditValueChanging 事件处理程序添加到您的文本编辑中):

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