Pass boolean value between custom forms - Windows Form

2

I am developing a Windows Form type solution that contains two forms:

Form frm_open_window :

Fields:

  • (1) Button "Open": Open the form called frm_nueva_ventana .

When this button is pressed, it must be locked. Example: Abrir.Enabled = false;

Form frm_nueva_ventana :

Fields:

  • (1) RadioButton: Determines if the form button frm_snap_window is enabled.
  • (1) Button Finish: Close the current form (frm_nueva_ventana) and you must enable the form button frm_open_window strong>.

I was reviewing this question , but it does not apply to the situation I describe in my question.

The flow of the program is as follows:

  • Press the button Abrir of the form frm_open_window .
  • The OnClick event of the Abrir button opens an instance of the frm_new_window form and locks the Abrir button.
  • In the form frm_new_window , you can choose to select the RadioButton .
  • In the current frm_new_window form, press the Finalizar button, sending the checked (boolean) of the RadioButton to the first form that is, frm_open_window .
  • Set the value of the attribute Enabled to the button Abrir of the form frm_open_window .
  • To summarize, the problem I have at this moment is to send a boolean value between these two forms ; When the second form finishes its functionality, it must close and enable the Abrir button again.

    This is my current code:

    Form code frm_open_window :

    /// Propiedad pública del formulario "para habilitar el botón (Abrir)".
    public bool EstadoBoton { get; set; }
    
    /// <summary>
    /// Abrir segundo formulario (frm_nueva_ventana).
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnAbrir_Click(object sender, EventArgs e)
    {
        frm_nueva_ventana frmNewVnt = new frm_nueva_ventana();
        frmNewVnt.Show();
        btnAbrir.Enabled = false;
    }
    

    Code frm_nueva_ventana :

    /// <summary>
    /// Cerrar actual formulario (frm_nueva_ventana) y enviar
    /// el checked del RadioButton al botón del primer formulario.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnFinalizar_Click(object sender, EventArgs e)
    {
        // frm_abrir_ventana aún está abierta y debe estarlo.
        // por lo que no sé tampoco cómo detectar este proceso.
        frm_abrir_ventana frmAbrVnt = new frm_abrir_ventana();
        frmAbrVnt.Show();
        frmAbrVtn.EstadoBoton = true;
    }
    

    How can I set this functionality correctly?

        
    asked by Mauricio Arias Olave 19.08.2016 в 01:03
    source

    2 answers

    3

    This may be a solution:

    In the form frm_open_window

    using System;
    using System.Windows.Forms;
    
    namespace NavigateForms
    {
        public partial class frm_abrir_ventana : Form
        {
            public frm_abrir_ventana()
            {
                InitializeComponent();
            }
    
            private void btnAbrir_Click(object sender, EventArgs e)
            {
                //Instancia de la nueva ventana
                frm_nueva_ventana frmNuevaVentana = new frm_nueva_ventana();
                //Mostramos el formulario
                frmNuevaVentana.Show();
                //Pasamos la instancia actual de este formulario por un metodo publico que
                //contiene el formulario frmNuevaVentana
                frmNuevaVentana.getFrm(this);
                //Inhabilitamos el botón
                btnAbrir.Enabled = false;
            }
    
            /// <summary>
            /// Obtiene el valor boleano del checkbox del formulario frmNuevaVentana
            /// </summary>
            /// <param name="boleano"></param>
            public void getBoolean(bool boleano)
            {
                btnAbrir.Enabled = boleano;
            }
        }
    }
    

    In the form frm_new_window

    using System;
    using System.Windows.Forms;
    
    namespace NavigateForms
    {
        public partial class frm_nueva_ventana : Form
        {
            /// <summary>
            /// Esta variable de tipo frm_abrir_ventana nos servira para almacenar la instancia
            /// de la "pagina principal"
            /// </summary>
            frm_abrir_ventana formInstancia = null;
    
            public frm_nueva_ventana()
            {
                InitializeComponent();
            }
    
            private void btnFinalizar_Click(object sender, EventArgs e)
            {       
               //Llamamos al metodo publico que se encuentra en el formulario frm_abrir_ventana y pasamos
               //el valor del checkedbox
               formInstancia.getBoolean(checkB.Checked);
    
                //cerramos este formulario
               this.Close();         
            }
    
            /// <summary>
            /// Este metodo publico lo ocupamos en el formulario frm_abrir_ventana para poder pasar la instancia de la
            /// "pagina principal"
            /// </summary>
            /// <param name="form"></param>
            public void getFrm(frm_abrir_ventana form)
            {
                formInstancia = form;
            }
        }
    }
    
        
    answered by 19.08.2016 / 01:41
    source
    2

    You do not have to create a new instance of frm_abrir_ventana when you want to return to the parent window, you must use the existing instance

    here Communicate Forms

    I explain on the subject

    but basically you must have the instance of the parent form (in this case frm_abrir_ventana ) from the form that you are opening frm_nueva_ventana to know where you have to send the data from one window to the other

    The parent window sends its instance to the other form

    public class frm_abrir_ventana
    {
        public bool EstadoBoton { get; set; }
    
    
        private void btnAbrir_Click(object sender, EventArgs e)
        {
            frm_nueva_ventana frmNewVnt = new frm_nueva_ventana(this);
            frmNewVnt.Show();
            btnAbrir.Enabled = false;
        }
    
    }
    

    The daughter window receives the instance and uses it to send the data

    public class frm_nueva_ventana
    {
        private frm_abrir_ventana frmAbrVnt;
    
        public frm_nueva_ventana()
        {
            InitialComponente();
        }
    
        public frm_nueva_ventana(frm_abrir_ventana frm) : this()
        {
            this.frmAbrVnt = frm;
        }
    
    
        private void btnFinalizar_Click(object sender, EventArgs e)
        {
            frmAbrVtn.EstadoBoton = true;
            this.Close();
        }
    
    }
    

    observe how in the form constructor the instance is passed so as not to have to create a new one, thus being able to pass the value from one side to the other

        
    answered by 19.08.2016 в 03:20