Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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 .

What I have tried:

C#
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

C#
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);          
        }
Posted
Updated 19-Jul-23 23:54pm
v2

1 solution

Hook up the LostFocus event to each TextBox, all pointing to the same event handler. Then a Form level field to track a TextBox. Something like:
C#
public Form1()
{
    InitializeComponent();

    textBox1.LostFocus += OnTextBox_LostFocus;
    textBox2.LostFocus += OnTextBox_LostFocus;
    textBox3.LostFocus += OnTextBox_LostFocus;
}

private TextBox LastAccessed = null;

private void OnTextBox_LostFocus(object sender, EventArgs e)
{
    LastAccessed = sender as TextBox;
}

Now you know who, you can do what you want.
 
Share this answer
 
Comments
Ankit Goel 20-Jul-23 5:44am    
@Graeme_Grant , i want to access it in the customdatagridview class . Pls modify your suggestion sir
Graeme_Grant 20-Jul-23 5:53am    
Add a public property to the customdatagridview, that would be the simplest solution. I've just given you the framework, you need to implement it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900