コード例 #1
0
        private async void btnAcceder_Click(object sender, EventArgs e)  //Metodo asincrono para acceder a pantalla principal
        {
            if (txtUsername.Text != "User name" && txtPassword.Text != "Password")
            {
                string rutaArchivoCompleta = PathA + "Usuarios.xlsx";


                //Ingreso User name y Password
                lblAsteriscoUser.Visible = false;
                txtUsername.ForeColor    = Color.Silver;
                lblArteriscoPass.Visible = false;
                txtPassword.ForeColor    = Color.Silver;
                if (File.Exists(rutaArchivoCompleta)) //Pregrunto si el archivo existe
                {
                    SLDocument ArchivoExcel = new SLDocument(rutaArchivoCompleta);
                    int        iRow         = 2;
                    while (!string.IsNullOrEmpty(ArchivoExcel.GetCellValueAsString(iRow, 1)))
                    {
                        if (txtUsername.Text == ArchivoExcel.GetCellValueAsString(iRow, 2) && txtPassword.Text == ArchivoExcel.GetCellValueAsString(iRow, 3)) //Recorro el archivo y pregunto si existen las credenciales
                        {
                            encontrado = true;
                            IdUser     = ArchivoExcel.GetCellValueAsDouble(iRow, 1); //Obtengo el Id de Usuario
                        }

                        iRow++;
                    }
                    //Verificar si las credenciales son correctas
                    if (encontrado)
                    {
                        loading = new Loading();
                        FormPrincipal frmprincipal = new FormPrincipal(IdUser); //Paso como parametro al Id de usuario al formulario principal

                        //frmprincipal.WindowState = FormWindowState.Maximized;
                        loading.Show(); // Mostrar el loading
                        Task oTask = new Task(TiempoEspera);
                        oTask.Start();  // Se ejecuta el metodo asincrono
                        await oTask;
                        if (loading != null)
                        {
                            loading.Close();
                            frmprincipal.Show();
                            this.Visible = false;
                            frmprincipal.btnPerfil_Click(sender, e);
                        } //Ocultar el loading
                    }
                    else
                    {
                        MessageBox.Show("¡El usuario o contraseña son incorrectos!");
                    }
                }
                else
                {
                    MessageBox.Show("¡Lo sentimos... No hay usuarios registrados!", "Información", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtUsername.Text = "User name";
                    txtPassword.Text = "Password";
                    txtPassword.UseSystemPasswordChar = false;
                }
            }
            else if (txtUsername.Text == "User name" && txtPassword.Text == "Password")
            {
                lblAsteriscoUser.Visible = true;
                txtUsername.ForeColor    = Color.Red;
                lblArteriscoPass.Visible = true;
                txtPassword.ForeColor    = Color.Red;
            }
            else if (txtUsername.Text == "User name" && txtPassword.Text != "Password")
            {
                lblAsteriscoUser.Visible = true;
                txtUsername.ForeColor    = Color.Red;
                lblArteriscoPass.Visible = false;
            }
            else if (txtUsername.Text != "User name" && txtPassword.Text == "Password")
            {
                lblAsteriscoUser.Visible = false;
                lblArteriscoPass.Visible = true;
                txtPassword.ForeColor    = Color.Red;
            }
            else if (txtUsername.Text == "" && txtPassword.Text == "Password")
            {
                lblAsteriscoUser.Visible = true;
                txtUsername.ForeColor    = Color.Red;
                lblArteriscoPass.Visible = true;
                txtPassword.ForeColor    = Color.Red;
            }
            else if (txtUsername.Text == "User name" && txtPassword.Text == "")
            {
                lblAsteriscoUser.Visible = true;
                txtUsername.ForeColor    = Color.Red;
                lblArteriscoPass.Visible = true;
                txtPassword.ForeColor    = Color.Red;
            }
        }