A more generic solution using an Extension method. This allows you to update the Text property of any control.
public static class ControlExtensions
{
public static void UpdateControlText(this Control control, string text)
{
if (control.InvokeRequired)
{
_ = control.Invoke(new Action<Control, string>(UpdateControlText), control, text);
}
control.Text = text;
}
public static void UpdateAsync(this Control control, Action<Control> action)
{
if(control.InvokeRequired)
{
_ = control.Invoke(new Action<Control, Action<Control>>(UpdateAsync), control, action);
}
action(control);
}
}
And you can use the methods like this:
TextBox1.UpdateControlText(string.Empty); // Just update the Text property
// Provide an action/callback to do whatever you want.
Label1.UpdateAsync(c => c.Text = string.Empty);
Button1.UpdateAsync(c => c.Text == "Login" ? c.Text = "Logout" : c.Text = "Login");
Button1.UpdateAsync(c => c.Enabled == false);