Beispiel #1
0
        // Botões

        private void btnCadastro_Click(object sender, EventArgs e)
        {
            string nome = this.edNome.Text;
            string cpf  = (this.edCPF.MaskCompleted) ? this.edCPF.Text : "";
            string tel  = (this.edTelefone.MaskCompleted) ? this.edTelefone.Text : "";

            if ((String.IsNullOrWhiteSpace(nome)) || (String.IsNullOrWhiteSpace(cpf)) || (String.IsNullOrWhiteSpace(tel)))
            {
                MessageBox.Show("Preencha os campos corretamente", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            int pos;

            if ((pos = FormCadastro.CpfExiste(this.Users, cpf)) >= 0)
            {
                MessageBox.Show("CPF já cadastrado!");
                list.Items[pos].Selected = true;
                list.Select();
                return;
            }

            User user = new User(++this.UltimoID, nome, cpf, tel);

            this.Users.Add(user);
            this.list.Items.Add(FormCadastro.ItemFromUser(user));

            this.edNome.Clear();
            this.edCPF.Clear();
            this.edTelefone.Clear();
        }
Beispiel #2
0
 private void menu_salvar_Click(object sender, EventArgs e)
 {
     if (String.IsNullOrEmpty(this.CurrentFile))
     {
         menu_salvarcomo_Click(sender, e);
     }
     FormCadastro.GravarUsuariosEmDisco(this.Users, this.CurrentFile);
 }
Beispiel #3
0
 private void menu_salvarcomo_Click(object sender, EventArgs e)
 {
     dlgSalvar.ShowDialog();
     if (String.IsNullOrEmpty(dlgSalvar.FileName))
     {
         return;
     }
     this.CurrentFile = dlgSalvar.FileName;
     FormCadastro.GravarUsuariosEmDisco(this.Users, this.CurrentFile);
     this.AtualizaStatusBar();
 }
Beispiel #4
0
        private static ListViewItem[] ItemsFromUserList(List <User> users)
        {
            ListViewItem[] items = new ListViewItem[users.Count];
            int            i     = 0;

            foreach (User user in users)
            {
                items[i++] = FormCadastro.ItemFromUser(user);
            }
            return(items);
        }
Beispiel #5
0
 private void menu_abrir_Click(object sender, EventArgs e)
 {
     dlgAbrir.ShowDialog();
     if (String.IsNullOrEmpty(dlgAbrir.FileName))
     {
         return;
     }
     this.CurrentFile = dlgAbrir.FileName;
     this.Users       = FormCadastro.CarregarUsuariosDeDisco(this.CurrentFile);
     this.UltimoID    = FormCadastro.MaxId(this.Users);
     this.list.Items.Clear();
     this.list.Items.AddRange(FormCadastro.ItemsFromUserList(this.Users));
     this.AtualizaStatusBar();
 }
Beispiel #6
0
        private void btnPesquisa_Click(object sender, EventArgs e)
        {
            string nome = this.edNome.Text;
            string cpf  = (this.edCPF.MaskCompleted) ? this.edCPF.Text : "";
            string tel  = (this.edTelefone.MaskCompleted) ? this.edTelefone.Text : "";

            int index = FormCadastro.PesquisarUsuario(this.Users, nome, cpf, tel);

            if (index <= -1)
            {
                MessageBox.Show("User not found");
                return;
            }
            list.Items[index].Selected = true;
            list.Select();
            list_SelectedIndexChanged(sender, e);
        }
Beispiel #7
0
        private static List <User> CarregarUsuariosDeDisco(string filename)
        {
            string     str;
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);

            using (StreamReader reader = new StreamReader(fs, Encoding.UTF8)) {
                str = reader.ReadToEnd();
            }

            List <User> users = new List <User>();

            int           count       = 0;
            bool          string_open = false;
            List <string> fields      = new List <string>();
            StringBuilder builder     = new StringBuilder();

            foreach (char c in str)
            {
                switch (c)
                {
                case '\n':
                case '\r':
                    if (fields.Count <= 0)
                    {
                        break;
                    }
                    if (!string_open)
                    {
                        fields.Add(builder.ToString());
                    }
                    if (count++ > 0)
                    {
                        User new_user = FormCadastro.CreateUserFromFields(fields);
                        if (new_user != null)
                        {
                            users.Add(new_user);
                        }
                    }
                    fields.Clear();
                    builder.Clear();
                    break;

                case '"':
                    string_open = !string_open;
                    break;

                case ',':
                    if (!string_open)
                    {
                        fields.Add(builder.ToString());
                        builder.Clear();
                        break;
                    }
                    builder.Append(c);
                    break;

                default:
                    builder.Append(c);
                    break;
                }
            }

            return(users);
        }