S
Steve K
I've got a full day invested in this now and I still can't make it work.
Hopefully someone here can offer some suggestions.
Here is the situation: I have a data entry application and am using a
DataGridView to enter order details. When it comes to the serial
numbers we will use a barcode reader which send a carriage return
character after each barcode is read. We can't change this behavior
(well, we can but it works great in the other 3-4 applications we use)
When a user is entering serial numbers into the serial number cell I
want each scanned serial number to be on it's own line, just like:
abc123
abc456
abc789
I'm able to get this behavior by pressing the SHIFT+ENTER combo but I
can't setup the barcode reader to send this combination.
The solution I've come up with (based on an article I found online
somewhere) is to create my own DGV Cell, Column and EditingControl.
Here is the code for those classes:
<code>
public class PMDDataGridViewTextBoxEditingControl :
DataGridViewTextBoxEditingControl
{
public override bool EditingControlWantsInputKey(Keys keyData, bool
dataGridViewWantsInputKey)
{
switch (keyData & Keys.KeyCode)
{
case Keys.Prior:
case Keys.Next:
case Keys.End:
case Keys.Home:
case Keys.Left:
case Keys.Up:
case Keys.Right:
case Keys.Down:
case Keys.Delete:
case Keys.Enter:
return true;
}
return base.EditingControlWantsInputKey(keyData,
dataGridViewWantsInputKey);
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.Text += Environment.NewLine;
this.Select(this.Text.Length, 0);
}
base.OnKeyDown(e);
}
}
public class PMDMultilinDataGridViewCell : DataGridViewTextBoxCell
{
public override Type EditType
{
get
{
return typeof(PMDDataGridViewTextBoxEditingControl);
}
}
}
public class PMDMultilineDataGridViewColumn : DataGridViewColumn
{
public PMDMultilineDataGridViewColumn()
: base(new PMDMultilinDataGridViewCell())
{
this.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
}
}
</code>
Note that in the EditingControl I am inserting a newline and moving the
cursor to this newline when an enter key is detected.
My question (finally!) is if this is a sound solution? It works, but
feels "hacky" to me and I suspect I may be missing a more elegant solution.
Anyone have any ideas?
Hopefully someone here can offer some suggestions.
Here is the situation: I have a data entry application and am using a
DataGridView to enter order details. When it comes to the serial
numbers we will use a barcode reader which send a carriage return
character after each barcode is read. We can't change this behavior
(well, we can but it works great in the other 3-4 applications we use)
When a user is entering serial numbers into the serial number cell I
want each scanned serial number to be on it's own line, just like:
abc123
abc456
abc789
I'm able to get this behavior by pressing the SHIFT+ENTER combo but I
can't setup the barcode reader to send this combination.
The solution I've come up with (based on an article I found online
somewhere) is to create my own DGV Cell, Column and EditingControl.
Here is the code for those classes:
<code>
public class PMDDataGridViewTextBoxEditingControl :
DataGridViewTextBoxEditingControl
{
public override bool EditingControlWantsInputKey(Keys keyData, bool
dataGridViewWantsInputKey)
{
switch (keyData & Keys.KeyCode)
{
case Keys.Prior:
case Keys.Next:
case Keys.End:
case Keys.Home:
case Keys.Left:
case Keys.Up:
case Keys.Right:
case Keys.Down:
case Keys.Delete:
case Keys.Enter:
return true;
}
return base.EditingControlWantsInputKey(keyData,
dataGridViewWantsInputKey);
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.Text += Environment.NewLine;
this.Select(this.Text.Length, 0);
}
base.OnKeyDown(e);
}
}
public class PMDMultilinDataGridViewCell : DataGridViewTextBoxCell
{
public override Type EditType
{
get
{
return typeof(PMDDataGridViewTextBoxEditingControl);
}
}
}
public class PMDMultilineDataGridViewColumn : DataGridViewColumn
{
public PMDMultilineDataGridViewColumn()
: base(new PMDMultilinDataGridViewCell())
{
this.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
}
}
</code>
Note that in the EditingControl I am inserting a newline and moving the
cursor to this newline when an enter key is detected.
My question (finally!) is if this is a sound solution? It works, but
feels "hacky" to me and I suspect I may be missing a more elegant solution.
Anyone have any ideas?