Here is a quick-n-dirty sample missing moving backwards.
using System.ComponentModel;
using System.Diagnostics;
#pragma warning disable CS8618
namespace WinFormsApp1;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Controls
.OfType<NoBeepTextBox>()
.ToList()
.ForEach(b => { b.TriggerEvent += OnTriggerEvent; });
}
private void OnTriggerEvent(bool last, string name)
{
if (last)
{
Debug.WriteLine($"Button {name} call to database save method");
}
}
}
/// <summary>
/// Place in a class file
/// </summary>
public class NoBeepTextBox : TextBox
{
/// <summary>
/// We really do not need name, here just to show you the correct
/// TextBox was used
/// </summary>
public delegate void TriggerDelegate(bool isLast, string name);
public event TriggerDelegate TriggerEvent;
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == (Keys.Return))
{
e.Handled = true;
e.SuppressKeyPress = true;
TriggerEvent?.Invoke(Last, Name);
if (!Last)
{
SendKeys.Send("{TAB}");
}
return;
}
base.OnKeyDown(e);
}
/// <summary>
/// Set the last TextBox to true in the form designer
/// </summary>
[Category("Behavior"), Description("Last TextBox")]
public bool Last { get; set; }
}