Pass data between forms

0

I'm developing my CBT and I needed to make the data collected in the register serve as a login, because we do not have a server yet to save this data, so they are being made with ArrayList .

Follow the code:

public partial class Frm_cadastro : Form
{
    ArrayList Nome = new ArrayList();
    ArrayList Senha = new ArrayList();
    ArrayList Email = new ArrayList();
    ArrayList DataNascimento = new ArrayList();
    int Contador= 0; 

    public Frm_cadastro()
    {
        InitializeComponent();
    }

    private void btn_cadastrar_Click(object sender, EventArgs e)
    {
        string nome, email, senha;
        nome = txt_nome.Text;
        email = txt_email.Text;
        senha = txt_senha.Text;
        if (nome.Trim().Length == 0 || email.Trim().Length == 0 || senha.Trim().Length == 0)
        {
            MessageBox.Show("Todos os campos devem estar preenchidos!", "Aviso");

        }
        else if (chk_termos.Checked == false)
        {
            MessageBox.Show("Os termos devem ser aceitos!");
        }
        else
        {
            Frm_confirmacao Confirmacao = new Frm_confirmacao();
            Confirmacao.Show();
            this.Hide();
        }
        Nome.Add(txt_nome.Text);
        Senha.Add(txt_senha);
        Email.Add(txt_email);
        DataNascimento.Add(dtp_data_nascimento.Text);
    }

    private void btn_voltar_Click(object sender, EventArgs e)
    {
        Form Principal = Application.OpenForms["frm_principal"];
        Principal.Show();
        this.Dispose();
    }

    private void Frm_cadastro_Load(object sender, EventArgs e)
    {

    }
}

and here the login screen, as I still could not make it capita the variables, I gave a specific condition, but this was not what I wanted

private void lnk_esqueceu_senha_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        Frm_esquecer_senha Senha = new Frm_esquecer_senha();
        Senha.Show();
        this.Hide();
    }

    private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        Frm_cadastro Cadastro = new Frm_cadastro();
        Cadastro.Show();
        this.Hide();
    }

    private void btn_entrar_Click(object sender, EventArgs e)
    {
        string email, senha;
        email = txt_login.Text;
        senha = txt_senha.Text;

        if (email.Trim().Length == 0 || senha.Trim().Length == 0)
        {
            MessageBox.Show("Todos os campos devem estar preenchidos!", "Aviso");
        }
        else if (email != "Administrador" && senha != "Adm@2014")
        {
            MessageBox.Show("E-mail ou senha incorretos!");

        }
        else
        {
            Frm_pagina_inicial Inicial = new Frm_pagina_inicial();
            Inicial.Show();
            this.Hide();
        }
    }

    private void txt_senha_TextChanged(object sender, EventArgs e)
    {
        txt_senha.PasswordChar = '*';
    }
    
asked by anonymous 24.04.2014 / 02:04

2 answers

2

A quick solution would be to create a static class to store the information you want, such as a list of emails, passwords and names, which would be accessible from anywhere in the system.

First thing: get ArrayList

And replace with List<string> and also pluralize the names of variables that represent lists. The initialization would look like this:

List<string> Nomes = new List<string>();
List<string> Senhas = new List<string>();
List<string> Emails = new List<string>();
List<string> DataNascimentos = new List<string>();

Move the lists to a static class

Create another file and move the lists to another class, making them static as well:

public static class Login
{
    public static List<string> Nomes = new List<string>();
    public static List<string> Senhas = new List<string>();
    public static List<string> Emails = new List<string>();
    public static List<string> DataNascimentos = new List<string>();
}

Change the way you add data to the list

In the part that says:

    Nome.Add(txt_nome.Text);
    Senha.Add(txt_senha);
    Email.Add(txt_email);
    DataNascimento.Add(dtp_data_nascimento.Text);

Switch By:

    Login.Nomes.Add(txt_nome.Text);
    Login.Senhas.Add(txt_senha.Text);
    Login.Emails.Add(txt_email.Text);
    Login.DataNascimentos.Add(dtp_data_nascimento.Text);

This way you can simply call Login.AlgumaCoisa and have the accessible lists of other classes.

    
13.05.2014 / 21:28
0

first, it would create a class for the login data:

public class Login
{
      public string Nome {get;set;}
      public string Senha {get;set;}
      public string Email {get;set;}
      public DateTime Nascimento {get;set;}
}

and in the program, would create the list of data:

public partial class Frm_cadastro : Form
{
    public static List<Login> Dados = new List<Login>();

    public Frm_cadastro()
    {
       InitializeComponent();
    }
}

However, you have to keep the list of data in the main form, which will be kept open during the entire execution of the program.

When registering a new login, add the list

In the registration form, you can access it using:

Login obj = new Login();
obj.Nome = textBoxNome.Text;
//outros campos

FormPrincipal.Dados.Add(obj);

In the login form, you can validate the login like this:

Login obj = FormPrincipal.Dados.Find(x=>x.Email == textBoxLoginEmail.Text);

if (obj != null)
{
   if (obj.Senha == textBoxLoginSenha.Text)
   {
     //Senha válida
   }
   else
   { 
    //Senha inválida
   }
}
else
{
    //email nao encotrado
}
    
14.04.2017 / 18:46