Pass data between Forms

Asked

Viewed 4,407 times

0

I am working on my TCC and I needed to make the data collected in the register serve as login, because we do not have a server yet to store this data, so they are being done with ArrayList.

Follows 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 haven’t been able to make it capitalize the variables, I gave a specific condition, but this is not how I wanted to

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 = '*';
    }
  • 2

    Are you sure you will use Arraylist? You are using . Net 1.1?

  • Yes because it is what we learn in class, and as we do not have bank we use the arraylist. Usk the VB 2010 but I do not know if it is the 1.1

  • If you are using Visual Studio 2010 (not VB 2010), you are using . Net 4.0. Since version 2.0 Arraylist has been replaced by List<T> because the previous one is considered unsafe and has performance problems. The ONLY reason to use Arraylist would be not to have a more current . Net available, which is not the case. Also, I see some other problems in the code, but in order to help you, we need more specific information about what the difficulty is. If more details no one will be able to help you.

  • give way to the question please

2 answers

2

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

First thing: take the ArrayList

And replace by List<string> and also pluralize the names of variables that represent lists. The initialization would then 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 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 to 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);

Exchange for:

    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);

That way you can just call Login.AlgumaCoisa and shall have accessible lists of other classes.

0

first, create a class for 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, create the data list:

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

    public Frm_cadastro()
    {
       InitializeComponent();
    }
}

however, you have to keep the data list in the main form, which will be kept open throughout the 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 thus:

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
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.