Articles in this section
Category / Section

How to restrict the multiline text insertion behavior in WinForms GridControl?

1 min read

Restrict the multiline insertion

To disable the multiline insertion on pressing Crtl+Enter in the OriginalTextBox cell, the CurrentCellKeyPress and CurrentCellKeyDown events are handled.

C#

bool disable_multiline = true;
void gridControl1_CurrentCellKeyPress(object sender, KeyPressEventArgs e)
{
    if ((Control.ModifierKeys & Keys.Control) != 0 && e.KeyChar.ToString() == "\n")
    {
        if (disable_multiline) //to check whether to disable or enable the insertion
        {                   
            e.Handled = true;
        }
    } 
}
void gridControl1_CurrentCellKeyDown(object sender, KeyEventArgs e)
{
    if ((Control.ModifierKeys & Keys.Control) != 0 && e.KeyCode == Keys.Enter)
    {
                       if (disable_multiline)
        {                   
            // Disabling multiline insertion.
            e.Handled = true;
        }
    }
}
// disable_multiline bool value assigned in button click.
private void EnableBtn_Click(object sender, EventArgs e)
{
    disable_multiline = false;
}
private void DisableBtn_Click(object sender, EventArgs e)
{
    disable_multiline = true;
}

 

VB

Private disable_multiline As Boolean = True
Private Sub gridControl1_CurrentCellKeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
 If (Control.ModifierKeys And Keys.Control) <> 0 AndAlso e.KeyChar.ToString() = Constants.vbLf Then
  If disable_multiline Then 'to check whether disable or enable the insertion
   e.Handled = True
  End If
 End If
End Sub
Private Sub gridControl1_CurrentCellKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
 If (Control.ModifierKeys And Keys.Control) <> 0 AndAlso e.KeyCode = Keys.Enter Then
  If disable_multiline Then
   e.Handled = True
  End If
 End If
End Sub
' disable_multiline bool value assigned in button click.
Private Sub EnableBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
 disable_multiline = False
End Sub
Private Sub DisableBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
 disable_multiline = True
End Sub

 

Samples:

C#: MultiLineInsertion-C#

VB: MultiLineInsertion-VB

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied