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 :
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
{
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 typeof(JxDataGridViewTextBoxEditingControl);
}
}
public override Type ValueType
{
get
{
return typeof(object);
}
}
public override object DefaultNewRowValue
{
get
{
return null;
}
}
}
class JxDataGridViewTextBoxEditingControl : JxTextBox2, IDataGridViewEditingControl
{
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;
public JxDataGridViewTextBoxEditingControl()
: base()
{
}
public object EditingControlFormattedValue
{
get
{
return this.Text;
}
set
{
this.Text = value.ToString();
}
}
public object GetEditingControlFormattedValue(
DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
public void ApplyCellStyleToEditingControl(
DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
}
public int EditingControlRowIndex
{
get
{
return rowIndex;
}
set
{
rowIndex = value;
}
}
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;
}
}
public void PrepareEditingControlForEdit(bool selectAll)
{
}
public bool RepositionEditingControlOnValueChange
{
get
{
return true;
}
}
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
valueChanged = value;
}
}
public Cursor EditingPanelCursor
{
get
{
return base.Cursor;
}
}
protected override void OnTextChanged(EventArgs eventargs)
{
valueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnTextChanged(eventargs);
}
}
}
If you need it too, here is the code of the JxTextBox2 used as EditingControl :
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 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;
}
}
[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.