Finding the sender for OnEnter event

rahul kumar 605 Reputation points
2023-07-12T10:30:16.3133333+00:00

Below is the code snippet for my customdatagridview .

  protected override void OnEnter(EventArgs e)
        {
            // datagridview has data 
            if (this.RowCount > 0)
            {
                if (// what condition)
                {
                    this.CurrentCell = this.Rows[0].Cells[0]; // select the first cell in the first row
                }
                else // second textbox causes 
                {
                    this.CurrentCell = this[0, this.RowCount - 1]; // select the last row's first cell
                }
               // rest of stuff
            base.OnEnter(e);
        }


Now on the windows forms , besides the above customdatagridview , I have two textboxes(they are also custom textboxes) on my form . both of them on pressing enter are causing the onenter event of this customdatagridview to get triggered . The scenario is I want to check which textbox causes the datagridview to trigger its onenter event . But the real problem is i do not access to these textboxes in this customdatagridview.cs file .

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,798 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,933 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 17,431 Reputation points Microsoft Vendor
    2023-07-13T01:44:56.8933333+00:00

    Hi @rahul kumar , Welcome to Microsoft Q&A.

    Updated:

    You can use tabindex comparison to determine which textbox it is from.

    The demo I gave in the question is just for show about the problem.

    Define a public datagridview in customtextbox, and assign mydatagridview to it in the main form, so it can be accessed normally.

    mydatagridview :

    using System;
    using System.Windows.Forms;
    
    namespace _7_18_cc
    {
        public partial class mydatagridview : DataGridView
        {
            public mydatagridview()
            {
                InitializeComponent();
                this.AllowUserToAddRows = false;
            }
    
            protected override void OnPaint(PaintEventArgs pe)
            {
                base.OnPaint(pe);
            }
            protected override void OnEnter(EventArgs e)
            {
                base.OnEnter(e);
            }
        }
    }
    

    customtextbox:

    using System.Windows.Forms;
    namespace _7_18_cc
    {
        public partial class customtextbox: TextBox
        {
            public DataGridView tmpData;
            public customtextbox()
            {
                base.AutoSize = false;
                this.BorderStyle = BorderStyle.None;
                this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9 F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
                InitializeComponent();
            }
            protected override void OnKeyDown(KeyEventArgs e)
            {
                if((e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down))
                {
                    if(!string.IsNullOrEmpty(this.Text))
                    {
                        e.Handled = true;
                        if(this.TabIndex < tmpData.TabIndex)
                        {
                            tmpData.Rows.Add();
                            tmpData.CurrentCell = tmpData.Rows[0].Cells[0];
                            tmpData.BeginEdit(true);
                        }
                    }
                    else if(tmpData.Rows.Count > 0 && this.TabIndex > tmpData.TabIndex)
                    {
                        tmpData.CurrentCell = tmpData.Rows[tmpData.Rows.Count - 1].Cells[0];
                        tmpData.BeginEdit(true);
                    }
                    return;
                }
                base.OnKeyDown(e);
            }
            protected override void OnPaint(PaintEventArgs pe)
            {
                base.OnPaint(pe);
            }
        }
    }
    

    MainForm:

    using System.Windows.Forms;
    
    namespace _7_19_x
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                customtextbox1.tmpData = mydatagridview1;
                customtextbox2.tmpData = mydatagridview1;
            }
        }
    }
    

    enter image description here

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful