Custom Editor - Missing first Keystroke

2 posts, 0 answers
  1. Richard
    Richard avatar
    1 posts
    Member since:
    Mar 2016

    Posted 22 Mar Link to this post

    I am evaluating RadGridview. I need to create lots of custom editors for my upcoming project. To begin with, I just created a simple texteditor with the following code (I referred you demo code for this) .The issue is, the control losing the first key stroke. Suppose I start to edit a column by typing "John" then the column only showing "ohn". 

    Did I miss something ??

     

    class MyTextEditor : BaseGridEditor
       {
           public RadFormProductList ListForm { get; set; }
           protected override RadElement CreateEditorElement()
           {
               var editor = new MyTextEditorElement();
               return editor;
           }
           public override object Value
           {
               get
               {
                   MyTextEditorElement editor = (MyTextEditorElement)this.EditorElement;
                   return editor.Text;
               }
               set
               {
                   MyTextEditorElement editor = (MyTextEditorElement)this.EditorElement;
                   if (value != null && value != DBNull.Value)
                   {
                       editor.Text = value.ToString();
                   }
                   else
                   {
                       editor.Text = "";
                   }
               }
           }
          
       }
     
       public class MyTextEditorElement : RadTextBoxElement
       {
           public MyTextEditorElement()
           {
               this.BackColor = Color.White;
               this.ShowBorder = false;
           }
     
           protected override Type ThemeEffectiveType
           {
               get
               {
                   return typeof(RadTextBoxEditorElement);
               }
           }
     
       }

  2. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 24 Mar Link to this post

    Hello Richard,

    Thank you for writing.

    RadGridView manages user mouse and keyboard input over its rows by GridRowBehavior. Depending on the row type, RadGridView introduces different behaviors. Thus, when start typing while the grid is not in edit mode, the relevant editor is activated and initialized with the entered text. This is performed in the ProcessAlphaNumericKey method of the GridRowBehavior when the editor is RadTextBoxEditor. However, your custom editor inherits BaseGridEditor. That is why the first character is not stored. You can find below a sample code snippet demonstrating how to initialize the custom editor with the whole entered text:
    public Form1()
    {
        InitializeComponent();
        this.radGridView1.EditorRequired += radGridView1_EditorRequired;
     
        //register the custom row  behavior
        BaseGridBehavior gridBehavior = radGridView1.GridBehavior as BaseGridBehavior;
        gridBehavior.UnregisterBehavior(typeof(GridViewDataRowInfo));
        gridBehavior.RegisterBehavior(typeof(GridViewDataRowInfo), new CustomGridDataRowBehavior());
    }
     
    public class CustomGridDataRowBehavior : GridDataRowBehavior
    {
        protected override bool ProcessAlphaNumericKey(KeyPressEventArgs keys)
        {
            bool result = base.ProcessAlphaNumericKey(keys);
            if (this.IsInEditMode &&
                (this.BeginEditMode == RadGridViewBeginEditMode.BeginEditOnKeystroke ||
                 this.BeginEditMode == RadGridViewBeginEditMode.BeginEditOnKeystrokeOrF2))
            {
                if (this.GridViewElement.ActiveEditor is MyTextEditor)
                {
                    this.GridViewElement.ActiveEditor.Value = keys.KeyChar;
                    if (this.GridViewElement.IsInEditMode)
                    {
                        MyTextEditor textBox = (MyTextEditor)this.GridViewElement.ActiveEditor;
                        RadTextBoxItem textBoxItem = ((RadTextBoxElement)textBox.EditorElement).TextBoxItem;
                        textBoxItem.SelectionStart = 1;
                        textBoxItem.SelectionLength = 0;
                    }
     
                    return true;
                }
            }
            return result;
        }
    }
     
    private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
    {
        e.Editor = new MyTextEditor();
    }
     
    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'nwindDataSet.Customers' table. You can move, or remove it, as needed.
        this.customersTableAdapter.Fill(this.nwindDataSet.Customers);
    }
     
    class MyTextEditor : BaseGridEditor
    {
        //public RadFormProductList ListForm { get; set; }
        protected override RadElement CreateEditorElement()
        {
            var editor = new MyTextEditorElement();
            return editor;
        }
        public override void BeginEdit()
        {
            base.BeginEdit();
            MyTextEditorElement editor = (MyTextEditorElement)this.EditorElement;
            editor.TextBoxItem.TextBoxControl.Focus();
        }
        public override object Value
        {
            get
            {
                MyTextEditorElement editor = (MyTextEditorElement)this.EditorElement;
                return editor.Text;
            }
            set
            {
                MyTextEditorElement editor = (MyTextEditorElement)this.EditorElement;
                if (value != null && value != DBNull.Value)
                {
                    editor.Text = value.ToString();
                }
                else
                {
                    editor.Text = "";
                }
            }
        }
    }
     
    public class MyTextEditorElement : RadTextBoxElement
    {
        public MyTextEditorElement()
        {
            this.BackColor = Color.White;
            this.ShowBorder = false;
        }
     
        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(RadTextBoxEditorElement);
            }
        }
    }

    I hope this information helps. Should you have further questions I would be glad to help.
     
    Regards,
    Dess
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top