I have a customtextbox and a customdatagridview . the customtextbox on its OnKeyDown event behaving as tab and on its back key behaving as reverse tab respectively . Now on my customdatagridview's OnEnter, I want to analyze which customtextbox is sending the focus to customdatagridview so that I change my current cell accordingly , but I am confused as how to get it .
public partial class betterdatagridview : DataGridView
{
protected override void OnEnter(EventArgs e)
{
// datagridview has data
if (this.RowCount > 0)
{
if () // what to place here to check which customtextbox send me here
{
this.CurrentCell = this.Rows[0].Cells[0]; // select the first cell in the first row
}
else if()
{
this.CurrentCell = this.Rows[RowCount - 1].Cells[0];//[0, this.RowCount - 1]; // select the last row's first cell
}
this.BeginEdit(true); // it must stay here
TextBox textBox = (TextBox)this.EditingControl; // Cast the EditingControl to TextBox
textBox.SelectionStart = textBox.Text.Length; // select the textbox from last
}
and below is the customtextbox
protected override void OnKeyDown(KeyEventArgs e)
{
if (!DisableFeatures)
{
if ((e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)&& this.Text !="")
{
e.Handled = true;
e.SuppressKeyPress = true;
TriggerEvent?.Invoke(Last, Name);
if (!Last)
{
SendKeys.Send("{TAB}");
}
return;
}
if ((e.KeyCode == Keys.Back || e.KeyCode == Keys.Up) && this.Text.Length == 0)
{
SendKeys.Send("+{TAB}");
}
}
base.OnKeyDown(e);
}