How to capture Mouse and Key events when the text box cell is in an active state

9 Dec 20192 minutes to read

The embedded text box control gets the mouse actions while the text box is active. You can subscribe to the embedded text box’s events inside a cell by accessing it through the cell’s renderer.

private void Form1_Load(object sender, System.EventArgs e)
{
    //Creates TextBoxCellRenderer object.
    GridTextBoxCellRenderer textBoxCellRenderer = (GridTextBoxCellRenderer) this.grid.CellRenderers["TextBox"];
    
    //Handle Renderer.TextBox.MouseDown to capture mouse events. 
    textBoxCellRenderer.TextBox.MouseDown += new MouseEventHandler(textbox_MouseDown);
    
    //Handle Renderer.TextBox.KeyUp to capture key events.
    textBoxCellRenderer.TextBox.KeyUp += new KeyEventHandler(textBox_KeyUp);
}

private void textbox_MouseDown(object sender, MouseEventArgs e)
{
    Console.WriteLine("textbox_MouseDown");
}

private void textBox_KeyUp(object sender, KeyEventArgs e)
{
    Console.WriteLine("textBox_KeyUp");
}
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
'Creates TextBoxCellRenderer object.
Dim textBoxCellRenderer As GridTextBoxCellRenderer = CType(Me.grid.CellRenderers("TextBox"), GridTextBoxCellRenderer)

'Handle Renderer.TextBox.MouseDown to capture mouse events.
AddHandler textBoxCellRenderer.TextBox.MouseDown, AddressOf textbox_MouseDown
AddHandler textBoxCellRenderer.TextBox.KeyUp, AddressOf textBox_KeyUp

'Form1_Load.
End Sub 

Private Sub textbox_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
Console.WriteLine("textbox_MouseDown")
End Sub 
	
Private Sub textBox_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
    Console.WriteLine("textBox_KeyUp")    
End Sub