Trabajo en una aplicación Windows Forms, C#, IoC ninject. Como dice la pregunta obtener datos desde un formulario nieto al padre(del padre se llama a un formulario hijo y este llama a otro formulario) y desde este ultimo se toma los datos y se le pasa al padre.
Este es fácil de resolver utilizando variables globales, pero es una mala practica de programación.
Lo que pude hacer es implementar eventos bajo un patrón Singleton guiandome de este link pasar datos entre formularios
Events
public static class CompleteEvent
{
public delegate void CompleteHandler(CompleteEventArgs arg);
public static event CompleteHandler Complete;
public static event CompleteHandler PasarDosVariables;
public static void RaiseEvent() { }
public static void RaiseEvent(string strValor)
{
//if (Complete != null)
// Complete(new CompleteEventArgs());
Complete?.Invoke(new CompleteEventArgs(strValor));
}
public static void RaiseEvent(int codigo, string valor)
{
PasarDosVariables?.Invoke(new CompleteEventArgs(codigo, valor));
}
}
public class CompleteEventArgs : EventArgs
{
public CompleteEventArgs() { }
public CompleteEventArgs(string strValor)
{
StrValor = strValor;
}
public CompleteEventArgs(int codigo, string valor)
{
this.Codigo = codigo;
this.StrValor = valor;
}
public int Codigo { get; set; }
public string StrValor { get; set; }
}
RecolectarDatos
public class RecolectarDatos
{
private static RecolectarDatos _datos;
private RecolectarDatos()
{
}
public static RecolectarDatos Instance()
{
//if (_controles == null)
// _controles = new ActivarControles();
//return _controles;
return _datos ?? (_datos = new RecolectarDatos());
}
public int Codigo { get; set; }
public string StrValor { get; set; }
}
Mi código que me da problemas cuando uso el evento CompleteEvent.PasarDosVariables
mas de una vez.
private void btnBuscarColor_Click(object sender, EventArgs e)
{
var argNombreEntidad = new ConstructorArgument("nombreEntidad", StrColour.NameEntity);
var argT = new ConstructorArgument("t", EnumAsignacionTablas.Colour);
var frm = CompositionRoot.Resolve<FrmInputBox>(argNombreEntidad, argT);
CompleteEvent.PasarDosVariables -= CompleteEvent_PasarDosVariables;
CompleteEvent.PasarDosVariables += CompleteEvent_PasarDosVariables;
frm.ShowDialog();
}
private void CompleteEvent_PasarDosVariables(CompleteEventArgs arg)
{
try
{
if (vBool)
{
if (arg.Codigo != 0 && !string.IsNullOrEmpty(arg.StrValor))
{
if (DetalleItemColour.Count != 1)
{
var entity = new Colour()
{
ColourId = arg.Codigo,
Nombre = arg.StrValor
};
_detalleColour.Add(entity);
dgvColor.AutoGenerateColumns = false;
dgvColor.DataSource = DetalleItemColour;
CompleteEvent.RaiseEvent();
arg.Codigo = 0;
arg.StrValor = String.Empty;
}
else
{
MessageBoxEx.EnableGlass = false;
MessageBoxEx.Show(this, "Solo se permite un solo Color", StrColour.NameEntity,
MessageBoxButtons.OK, MessageBoxIcon.Information);
vBool = false;
arg.Codigo = 0;
arg.StrValor = String.Empty;
}
}
}
}
catch (Exception e)
{
MessageBoxEx.Show(this, $"Error:{e.Message}", "Error Inesperado", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void txtMarca_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F3)
{
var argNombreEntidad = new ConstructorArgument("nombreEntidad", StrMarca.NameEntity);
var argT = new ConstructorArgument("t", EnumAsignacionTablas.Marca);
var frm = CompositionRoot.Resolve<FrmInputBox>(argNombreEntidad, argT);
CompleteEvent.PasarDosVariables -= CompleteEvent_PasarDosVariables1;
CompleteEvent.PasarDosVariables += CompleteEvent_PasarDosVariables1;
frm.ShowDialog();
}
}
private void CompleteEvent_PasarDosVariables1(CompleteEventArgs arg)
{
if (arg.Codigo != 0 && !string.IsNullOrEmpty(arg.StrValor))
{
txtMarcaId.Text = Convert.ToString(arg.Codigo);
txtMarca.Text = arg.StrValor;
CompleteEvent.RaiseEvent();
arg.Codigo = 0;
arg.StrValor = string.Empty;
}
}
El problema es que cuando utilizo un evento se activa el otro evento y me chanca los datos.