- Enter event is fired when itself or descendant controls got focus of windows operation system.
- EditingControl get focus at BeginEdit.
- EditingControl is child control in the DataGridView.
The reason of first OnEnter is because the DataGridView got focus.
The second reason is because the child control got focus.
You should be able to watch that call OnEnter in UserControl when TextBox got focus with below code
class TestControl : UserControl
{
public TestControl()
{
this.BackColor = Color.Pink;
this.Height = 100;
this.Width = 200;
var childTextBox = new TextBox() { Height = 50, Width = 100, Multiline = true };
this.Controls.Add(childTextBox);
}
private int count;
protected override void OnEnter(EventArgs e)
{
int i = ++count;
System.Diagnostics.Debug.WriteLine($"OnEnter A{i}\t{DateTime.Now: HH:mm:ss.fff}\t{this.Focused}");
this.Controls[0].Focus();
base.OnGotFocus(e);
System.Diagnostics.Debug.WriteLine($"OnEnter B{i}\t{DateTime.Now: HH:mm:ss.fff}\t{this.Focused}");
}
}
This behavior is by design of WinForms.
If you call BeginEnter in OnCellEnter instead of OnEnter, it will be called only once.
Because the focus of cell is virtual focus in the DataGridView.