private void setData(Pessoa.Model.Pessoa p) { pessoa = p; pessoaWithoutModify = p; nomeTb.Text = p.nome; cpfTb.Text = p.cpf; dataNascimentoTb.Text = p.dataNascimento.ToString(); cepTb.Text = p.endereco != null ? p.endereco.cep : ""; numeroTb.Text = p.endereco != null?p.endereco.numero.ToString() : ""; if (p.GetType().Name.Contains(typeof(Pessoa.Model.Aluno).Name)) { tipoCb.SelectedItem = "Aluno"; Pessoa.Model.Aluno aluno = (Model.Aluno)alunoDao.getById(p.id); matriculaTb.Text = aluno.matricula; } else if (p.GetType().Name.Contains(typeof(Pessoa.Model.Usuario).Name)) { tipoCb.SelectedItem = "Usuário"; Model.Usuario usuario = (Model.Usuario)usuarioDao.getById(p.id); loginTb.Text = usuario.login; senhaTb.Text = usuario.senha; } else { tipoCb.SelectedItem = "Autor"; Model.Autor autor = (Model.Autor)autorDao.getById(p.id); siteTb.Text = autor.site; } handleFields(); }
private string getTipoPessoa(Pessoa.Model.Pessoa p) { if (p.GetType().Name.Contains(typeof(Pessoa.Model.Usuario).Name)) { return("Usuário"); } else if (p.GetType().Name.Contains(typeof(Pessoa.Model.Aluno).Name)) { return("Aluno"); } else { return("Autor"); } }
private void handleFields() { string tipo = tipoCb.SelectedItem.ToString(); switch (tipo.ToLower()) { case "usuário": matriculaTb.Enabled = false; siteTb.Enabled = false; loginTb.Enabled = true; senhaTb.Enabled = true; loginLb.Text += " *"; senhaLb.Text += " *"; matriculaLb.Text = matriculaLb.Text.Replace(" *", ""); //O patch abaixo foi feito para o caso de edição e criação //de qualquer tipo de pessoa no mesmo formulario //Se nao for nulo if (pessoa != null) { //Se o tipo for diferente de usuario if (pessoa.GetType() != typeof(Model.Usuario)) { //Troca para usuario, criando uma nova instancia pessoa = new Model.Usuario(); } } else { //Caso seja nulo instancia um novo usuario pessoa = new Model.Usuario(); } break; case "aluno": siteTb.Enabled = false; loginTb.Enabled = false; senhaTb.Enabled = false; matriculaTb.Enabled = true; if (pessoa != null) { if (pessoa.GetType() != typeof(Model.Aluno)) { pessoa = new Model.Aluno(); } } else { pessoa = new Model.Aluno(); } loginLb.Text = loginLb.Text.Replace(" *", ""); senhaLb.Text = senhaLb.Text.Replace(" *", "");; matriculaLb.Text += " *"; break; case "autor": siteTb.Enabled = true; loginTb.Enabled = false; senhaTb.Enabled = false; matriculaTb.Enabled = false; if (pessoa != null) { if (pessoa.GetType() != typeof(Model.Autor)) { pessoa = new Model.Autor(); } } else { pessoa = new Model.Autor(); } loginLb.Text = loginLb.Text.Replace(" *", ""); senhaLb.Text = senhaLb.Text.Replace(" *", ""); matriculaLb.Text = matriculaLb.Text.Replace(" *", ""); break; default: siteTb.Enabled = false; loginTb.Enabled = false; senhaTb.Enabled = false; matriculaTb.Enabled = false; pessoa = null; break; } }
private void enviarBtn_Click(object sender, EventArgs e) { if (this.validateForm()) { Endereco.Model.Endereco endereco = new Endereco.Model.Endereco(); pessoa.nome = nomeTb.Text; pessoa.cpf = cpfTb.Text; endereco.cep = cepTb.Text; if (dataNascimentoTb.MaskFull) { pessoa.dataNascimento = DateTime.Parse(dataNascimentoTb.Text); } if (!numeroTb.Text.Equals("")) { endereco.numero = int.Parse(numeroTb.Text); } pessoa.endereco = endereco; if (pessoa.GetType().Name.Contains(typeof(Pessoa.Model.Usuario).Name)) { Model.Usuario usuario = (Model.Usuario)pessoa; usuario.login = loginTb.Text; usuario.senha = senhaTb.Text; if (opt == App.CrudOpt.CADASTRAR) { pessoaDao.add(usuario); } else { if (pessoaWithoutModify.GetType() != usuario.GetType()) { pessoaDao.delete(pessoaWithoutModify); pessoaDao.add(usuario); } else { pessoaDao.update(usuario); } } pessoaDao.saveChanges(); } else if (pessoa.GetType().Name.Contains(typeof(Pessoa.Model.Aluno).Name)) { Model.Aluno aluno = (Model.Aluno)pessoa; aluno.matricula = matriculaTb.Text; if (opt == App.CrudOpt.CADASTRAR) { pessoaDao.add(aluno); } else { if (pessoaWithoutModify.GetType() != aluno.GetType()) { pessoaDao.delete(pessoaWithoutModify); pessoaDao.add(aluno); } else { pessoaDao.update(aluno); } } pessoaDao.saveChanges(); } else { Model.Autor autor = (Model.Autor)pessoa; autor.site = siteTb.Text; if (opt == App.CrudOpt.CADASTRAR) { pessoaDao.add(autor); } else { if (pessoaWithoutModify.GetType() != autor.GetType()) { pessoaDao.delete(pessoaWithoutModify); pessoaDao.add(autor); } else { pessoaDao.update(autor); } } pessoaDao.saveChanges(); } if (Owner != null) { PessoaForm pf = (PessoaForm)Owner; pf.clean(); } MessageBox.Show("Salvo com sucesso !"); this.Close(); } }