Create variable that can be passed from one Windows Form to another and return it to the Main Form

3

Good morning,

I am working as Windows Forms and user controls within C #, which I am working with an application that acts as a clock watch.

This first screen acts, like the main screen. Which all users have access to now.

Pressing the "User" menu opens a new Form because the following modules can only be entered by "super-users". The screen that is displayed is as follows. Which acts as a login.

What I intend to do is that when logging in correctly, a session is maintained, until another button is pressed so that it can be finalized.

What you try to do is pass a variable from one Form to another with the following code:

Here is the Main Form:

bool presionar = true;

    private void usuarioToolStripMenuItem_Click(object sender, EventArgs e)
            {

              if(presionar== true) { 
                Login login = new Login(presionar);
                login.ShowDialog();

                // Si el login es correcto, procedo con la apetura normal de la aplicacion
                if (login.DialogResult == DialogResult.OK)
                {

                }

                }
 else
                    {

                    }
               }

This is the Login:

public partial class Login : Form
    {
        public Login( bool presionar)
        {
            InitializeComponent();
            this.presionar2 = presionar;

        }
        bool presionar2;

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "")
            {
                MessageBox.Show("Debes llenar todos los campos");
            }
            else
            {
                if (textBox1.Text == "Administrador" && textBox2.Text == "Admin")
                {
                    this.DialogResult = DialogResult.OK;

                    MessageBox.Show("Datos Correctos");
                    this.Close();
                    presionar2 = false;
                }
                else
                {

                    MessageBox.Show("Datos incorrectos");
                    textBox1.Clear();
                    textBox2.Clear();
                }

            }
        }

Up to here the variable press indicates if the button is "true or false" the variable if it passes until the time of the "Main Form" to the "Form Login" but how the value of the variable click on the "Form Login" to the "Main Form" or vice versa.

    
asked by Ezequie Lopez 27.08.2018 в 18:29
source

1 answer

2

You add a new class to your project that has the user's properties in this way:

public class Usuario
{
    public static string USERNAME;
    public static string PASSWORD;
    public static string FORMULARIO;
    public static bool PRESIONAR;
}

You set the values and access them at any time while the program is running. You just have to make reference to the class and that's it.

    
answered by 27.08.2018 / 18:38
source