Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I created a Custom Control and I want it as EditingControl in a DataGridView. All work fine but, I'm not able to catch the Enter Key in my Custom EditingControl.

The EditingControlWantsInputKey is implemented but is never called when I press the Enter key. The others keys (as Up and Down) works well.

I can't find the issue :(
Here's the code of the column, cell and EditingControl :

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing.Design;

namespace Jinxs_Framework
{
    public class JxDataGridViewTextBoxColumn : DataGridViewColumn
    {
        public JxDataGridViewTextBoxColumn()
            : base(new JxDataGridViewTextBoxCell())
        {
            this.CellTemplate = new JxDataGridViewTextBoxCell();
        }

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                // Ensure that the cell used for the template is a JxDataGridViewTextBoxCell.
                if (value != null &&
                    !value.GetType().IsAssignableFrom(typeof(JxDataGridViewTextBoxCell)))
                {
                    throw new InvalidCastException("Must be a JxDataGridViewTextBoxCell");
                }
                base.CellTemplate = value;
            }
        }

        public override object Clone()
        {
            JxDataGridViewTextBoxColumn col = (JxDataGridViewTextBoxColumn)base.Clone();
            col.AutoCompleteProperties = this.AutoCompleteProperties.Clone();
            return col;
        }

        public JxTextBox2.AutoCompletePropertiesClass ioAutoCompleteProperties = new JxTextBox2.AutoCompletePropertiesClass();
        public JxTextBox2.AutoCompletePropertiesClass AutoCompleteProperties
        {
            get
            {
                return ioAutoCompleteProperties;
            }
            set
            {
                ioAutoCompleteProperties = value;
            }
        }
    }

    public class JxDataGridViewTextBoxCell : DataGridViewTextBoxCell
    {

        public JxDataGridViewTextBoxCell()
            : base()
        {
        }

        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
            JxDataGridViewTextBoxColumn loCol = (JxDataGridViewTextBoxColumn)this.OwningColumn;
            JxDataGridViewTextBoxEditingControl ctl = DataGridView.EditingControl as JxDataGridViewTextBoxEditingControl;
            ctl.AutoCompleteProperties = loCol.AutoCompleteProperties.Clone();
            ctl.Text = this.Value.ToString();
        }

        public override void PositionEditingControl(bool setLocation, bool setSize, System.Drawing.Rectangle cellBounds, System.Drawing.Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow)
        {
            JxDataGridViewTextBoxEditingControl ctl = this.DataGridView.EditingControl as JxDataGridViewTextBoxEditingControl;
            if (ctl.DroppedDown)
            {
                cellBounds.Height = ctl.Height + 1;
                cellClip.Height = ctl.Height + 1;
            }
                
            base.PositionEditingControl(setLocation, setSize, cellBounds, cellClip, cellStyle, singleVerticalBorderAdded, singleHorizontalBorderAdded, isFirstDisplayedColumn, isFirstDisplayedRow);
        }
        
        public override Type EditType
        {
            get
            {
                // Return the type of the editing contol that JxDataGridViewTextBoxCell uses.
                return typeof(JxDataGridViewTextBoxEditingControl);
            }
        }

        public override Type ValueType
        {
            get
            {
                // Return the type of the value that contains.
                return typeof(object);
            }
        }

        public override object DefaultNewRowValue
        {
            get
            {
                // Use the current date and time as the default value.
                return null;
            }
        }
    }

    class JxDataGridViewTextBoxEditingControl : JxTextBox2, IDataGridViewEditingControl
    {
        DataGridView dataGridView;
        private bool valueChanged = false;
        int rowIndex;

        public JxDataGridViewTextBoxEditingControl()
            : base()
        {
        }

        // Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
        // property.
        public object EditingControlFormattedValue
        {
            get
            {
                return this.Text;
            }
            set
            {
                this.Text = value.ToString();
            }
        }

        // Implements the 
        // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
        public object GetEditingControlFormattedValue(
            DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue;
        }

        // Implements the 
        // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
        public void ApplyCellStyleToEditingControl(
            DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
        }

        // Implements the IDataGridViewEditingControl.EditingControlRowIndex 
        // property.
        public int EditingControlRowIndex
        {
            get
            {
                return rowIndex;
            }
            set
            {
                rowIndex = value;
            }
        }

        // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 
        // method.
        public bool EditingControlWantsInputKey(
            Keys key, bool dataGridViewWantsInputKey)
        {
            switch (key & Keys.KeyCode)
            {
                case Keys.Enter:
                case Keys.Up:
                case Keys.Down:
                    return true;
                default:
                    return !dataGridViewWantsInputKey;
            }
        }

        // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
        // method.
        public void PrepareEditingControlForEdit(bool selectAll)
        {
            // No preparation needs to be done.
        }

        // Implements the IDataGridViewEditingControl
        // .RepositionEditingControlOnValueChange property.
        public bool RepositionEditingControlOnValueChange
        {
            get
            {
                return true;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingControlDataGridView property.
        public DataGridView EditingControlDataGridView
        {
            get
            {
                return dataGridView;
            }
            set
            {
                dataGridView = value;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingControlValueChanged property.
        public bool EditingControlValueChanged
        {
            get
            {
                return valueChanged;
            }
            set
            {
                valueChanged = value;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingPanelCursor property.
        public Cursor EditingPanelCursor
        {
            get
            {
                return base.Cursor;
            }
        }

        protected override void OnTextChanged(EventArgs eventargs)
        {
            // Notify the DataGridView that the contents of the cell
            // have changed.
            valueChanged = true;
            this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
            base.OnTextChanged(eventargs);
        }
    }
}


If you need it too, here is the code of the JxTextBox2 used as EditingControl :

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Jinxs_Framework
{
    public partial class JxTextBox2 : UserControl
    {
        public JxTextBox2()
        {
            InitializeComponent();
            listBox1.Visible = false;
        }

        void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (AutoCompleteProperties.Mode == JxTextBoxAutoCompleteMode.Suggest)
                showSuggest();

            base.Text = textBox1.Text;
        }

        protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);

            hideSuggest();
        }

        private void showSuggest()
        {
            if (AutoCompleteProperties.Source == JxTextBoxAutoCompleteSource.CustomSource)
            {
                listBox1.Items.Clear();

                if (!String.IsNullOrEmpty(textBox1.Text))
                {
                    if (ioAutoCompleteProperties.CustomSource is IEnumerable<string>)
                    {
                        foreach (string loString in (IEnumerable<string>)ioAutoCompleteProperties.CustomSource)
                        {
                            if (loString.ToLower().Contains(textBox1.Text.ToLower()))
                                listBox1.Items.Add(loString);
                        }
                    }
                    else if (ioAutoCompleteProperties.CustomSource is BindingSource)
                    {
                        foreach (object loValue in (BindingSource)AutoCompleteProperties.CustomSource)
                        {
                            string lsValue;
                            if (!String.IsNullOrEmpty(AutoCompleteProperties.DataMember))
                                lsValue = ((DataRowView)loValue)[AutoCompleteProperties.DataMember].ToString();
                            else
                                lsValue = loValue.ToString();
                            if (lsValue.ToLower().Contains(textBox1.Text.ToLower()))
                                listBox1.Items.Add(lsValue);
                        }
                    }

                    if (listBox1.Items.Count > 0)
                    {
                        listBox1.Height = (listBox1.Items.Count + 1) * listBox1.ItemHeight;
                        listBox1.Visible = true;
                    }
                    else
                        listBox1.Visible = false;
                }
                else
                    listBox1.Visible = false;
            }
        }

        private void hideSuggest()
        {
            listBox1.Visible = false;
        }

        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                textBox1.Text = value;
            }
        }

/*
        public new EventHandler TextChanged;

        
        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
        }*/

        public AutoCompletePropertiesClass ioAutoCompleteProperties = new AutoCompletePropertiesClass();
        public AutoCompletePropertiesClass AutoCompleteProperties
        {
            get
            {
                return ioAutoCompleteProperties;
            }
            set
            {
                ioAutoCompleteProperties = value;
            }
        }

        public enum JxTextBoxAutoCompleteMode
        {
            None,
            Suggest
        }

        public enum JxTextBoxAutoCompleteSource
        {
            None,
            CustomSource
        }

        public bool DroppedDown
        {
            get
            {
                return listBox1.Visible;
            }
        }

        [TypeConverter(typeof(ExpandableObjectConverter))]
        public class AutoCompletePropertiesClass
        {
            public AutoCompletePropertiesClass Clone()
            {
                AutoCompletePropertiesClass loProp = new AutoCompletePropertiesClass();
                loProp.CustomSource = this.CustomSource;
                loProp.DataMember= this.DataMember;
                loProp.Mode = this.Mode;
                loProp.Source = this.Source;
                return loProp;
            }

            JxTextBoxAutoCompleteMode ieMode = JxTextBoxAutoCompleteMode.None;
            [DefaultValue(JxTextBoxAutoCompleteMode.None)]
            public JxTextBoxAutoCompleteMode Mode
            {
                get
                {
                    return ieMode;
                }
                set
                {
                    ieMode = value;
                }
            }

            JxTextBoxAutoCompleteSource ieSource = JxTextBoxAutoCompleteSource.None;
            [DefaultValue(JxTextBoxAutoCompleteSource.None)]
            public JxTextBoxAutoCompleteSource Source
            {
                get
                {
                    return ieSource;
                }
                set
                {
                    ieSource = value;
                }
            }



            string isDataMember = "";
            [TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
            [DefaultValue("")]
            [Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
            public string DataMember
            {
                get
                {
                    return isDataMember;
                }
                set
                {
                    isDataMember = value;
                }
            }

            object ioCustomSource = null;
            [DefaultValue(null)]
            [RefreshProperties(RefreshProperties.Repaint)]
            [AttributeProvider(typeof(IListSource))]
            public object CustomSource
            {
                get
                {
                    return ioCustomSource;
                }
                set
                {
                    ioCustomSource = value;
                }
            }

            /// <summary>
            /// Property masked because it's only used for the DataMemberFieldEditor in Designer.
            /// Use AutoCompleteCustomSource instead.
            /// </summary>
            [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
            public object DataSource
            {
                get
                {
                    return CustomSource;
                }
            }
        }

        private void listBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                textBox1.Text = listBox1.SelectedItem.ToString();
                hideSuggest();
            }
        }
        
        protected override bool IsInputKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Enter:
                    return true;
                default:
                    return base.IsInputKey(keyData);
            }
        }
        
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Down)
            {
                if ((listBox1.Visible) && (listBox1.Items.Count > 0))
                {
                    if (listBox1.SelectedIndex < listBox1.Items.Count - 1)
                        listBox1.SelectedIndex++;
                    else if (listBox1.SelectedIndex == -1)
                        listBox1.SelectedIndex = 0;
                }
                else
                    if (AutoCompleteProperties.Mode == JxTextBoxAutoCompleteMode.Suggest)
                        showSuggest();
            }
            else if ((e.KeyData == Keys.Up) && (listBox1.Visible) && (listBox1.Items.Count > 0))
            {
                if (listBox1.SelectedIndex > 0)
                    listBox1.SelectedIndex--;
            }
            else if ((e.KeyData == Keys.Enter) && (listBox1.Visible) && (listBox1.SelectedIndex > -1))
            {
                textBox1.Text = listBox1.SelectedItem.ToString();
                textBox1.SelectAll();
                hideSuggest();
                e.SuppressKeyPress = true;
            }
        }
    }
}


Thanks for your help, and for this website that is really a precious help for developpers :)
Bye.
Posted
Comments
Mayur Panchal 17-May-13 6:05am    
I think u should handle OnPreviewKeydown event to catch 'Enter' key in user control and write your stuff there.
alljinx 17-May-13 6:30am    
So simply... Thanks a lot !

1 solution

I think u should handle OnPreviewKeydown event to catch 'Enter' key in user control and write your stuff there.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900