How to put a cell in overstrike mode in WinForms GridControl?
Overstrike the textbox cell
Enabling
overstrike means to replace the existing characters with a new character as you
type in a cell. The idea of doing this is to select one character when a key is
pressed, and ignoring the backspace key.
Solution
This can be achieved by using the TextBox.SelectionLength property of the GridTextBoxCellRenderer in the CurrentCellKeyPress event. The Backspace key can optionally be translated to the Left Arrow key in the CurrentCellKeyDown event when you want the Backspace to move to the left without deleting characters.
//Hooking the Events in the Form_Load.
this.gridControl1.CurrentCellKeyPress += gridControl1_CurrentCellKeyPress;
this.gridControl1.CurrentCellKeyDown += gridControl1_CurrentCellKeyDown;
void gridControl1_CurrentCellKeyPress(object sender, KeyPressEventArgs e)
{
GridTextBoxCellRenderer cr = this.gridControl1.CurrentCell.Renderer as GridTextBoxCellRenderer;
if (e.KeyChar != Convert.ToChar(Keys.Back) && cr.TextBox.SelectionLength == 0)
{
//Programatically selecting One char.
cr.TextBox.SelectionLength = 1;
}
}
void gridControl1_CurrentCellKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back)
{
//Translating the Backspace key to the left arrow key.
SendKeys.Send("{LEFT}");
e.Handled = true;
}
}'Hooking the Events in the Form_Load.
AddHandler Me.gridControl1.CurrentCellKeyPress, AddressOf gridControl1_CurrentCellKeyPress
AddHandler Me.gridControl1.CurrentCellKeyDown, AddressOf gridControl1_CurrentCellKeyDown
Private Sub gridControl1_CurrentCellKeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
Dim cr As GridTextBoxCellRenderer = TryCast(Me.gridControl1.CurrentCell.Renderer, GridTextBoxCellRenderer)
If e.KeyChar <> Convert.ToChar(Keys.Back) AndAlso cr.TextBox.SelectionLength = 0 Then
'Programatically selecting One char.
cr.TextBox.SelectionLength = 1
End If
End Sub
Private Sub gridControl1_CurrentCellKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
If e.KeyCode = Keys.Back Then
'Translating the Backspace key to a left arrow key
SendKeys.Send("{LEFT}")
e.Handled = True
End If
End SubSamples:
C#: OverStrikeTextBoxCell-CS.zip
VB: OverStrikeTextBoxCell-VB.zip
Conclusion
I hope you enjoyed learning about how to put a cell in overstrike mode in WinForms GridControl.
You can refer to our WinForms GridControl feature tour page to know about its other groundbreaking feature representations and WinForms GridControl documentation, and how to quickly get started for configuration specifications.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!