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;
}
}
}
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.