Search code examples
c#devexpresstextedit

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


Solution

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