I'm working on a Windows Forms app in which I have a form with a button and a TextBox what I want is that when I press the button to do a process when I finish giving the focus to a TextBox.
The code I use is the following:
private void OnAplicarDescuento(SelectedAplicarDescuento obj)
{
if (obj.Descuento >= 0 && obj.PrecioVenta != 0)
{
txtDescuento.Text = $"{obj.Descuento:N2}";
txtPrecioVenta.Text = $"{obj.PrecioVenta:N2}";
// Trato de darle el foco al control Textbox.
Utilidades.ChangeControlStyles(btnAplicarDescuento, ControlStyles.StandardClick, false);
ActiveControl = txtPrecioVenta;
txtPrecioVenta.Select();
}
}
Method
// Para perder el foco de un control
public static void ChangeControlStyles(Control ctrl, ControlStyles flag, bool value)
{
MethodInfo method = ctrl.GetType().GetMethod("SetStyle", BindingFlags.Instance | BindingFlags.NonPublic);
if (method != null)
method.Invoke(ctrl, new object[] { flag, value });
}
The image where I want to remove the focus from the button is only the focus when I click on another control.