[ad_1]
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: <pre> 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); }
解決策 1
接続してください LostFocus
それぞれのイベント TextBox
、すべて同じイベント ハンドラーを指します。 次に、フォームレベルのフィールドを追跡します。 TextBox
。 何かのようなもの:
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; }
これで、誰が誰であるかがわかりました。好きなことを行うことができます。
[ad_2]
コメント