By the time the click event for the button has fired, the button now has focus instead of the textbox. So you need to capture which textbox last had the focus and use that.
Here is a rough and quick implementation, which should work as long as all your textboxes are on the form on load. It will work even with textboxes which aren't a direct child of the form (for example, contained within a panel or tab page):
public IEnumerable<Control> GetAll(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetAll(ctrl, type))
.Concat(controls)
.Where(c => c.GetType() == type);
}
private TextBox lastFocussedTextbox;
private void Form1_Load(object sender, EventArgs e)
{
foreach(TextBox textbox in GetAll(this, typeof(TextBox)))
{
textbox.LostFocus += (_s, _e) => lastFocussedTextbox = textbox;
}
}
private void button1_Click(object sender, EventArgs e)
{
if(lastFocussedTextbox != null)
{
lastFocussedTextbox.Text = button1.Text;
}
}
Credit for GetAll function: https://stackoverflow.com/a/3426721/13660130