Save to txt file and recover in a combobox

Asked

Viewed 669 times

2

This is what I need to do:

  1. Registering a customer on the form.
  2. Save the nome of clients that are registered in a txt file on the computer, example:

    • John
    • Marcos
    • Willian
  3. Load these names into a comboBox in another form.

Client form code, so far I was able to create the txt file and put the names, I just did not understand how to show in the combobox now.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace ProjetoFinal
{
    public partial class frmCadastroClientes : Form
    {
        int i = 0;
        int poc;

        public frmCadastroClientes()
        {
            InitializeComponent();
        }

        private void btnGravar_Click(object sender, EventArgs e)
        {
            i++;
            dgvClientes.Rows.Add(i, txtNome.Text, txtEnd.Text, txtCidade.Text, txtEstado.Text, maskFone.Text, txtEmail.Text);


            //Salvar clientes no TXT
        using (StreamWriter writer = new StreamWriter(@"C:\\Users\\willian\\Downloads\\dbClientes.txt", true))
        {
            writer.WriteLine(txtNome.Text);
        }


                /**
                String nome = txtNome.Text;
                frmOrdemServico os = new frmOrdemServico();
                os.MostrarClientes(nome);




                int tamanho = 50;
                string[] nomes = new string[tamanho];


                for(int i = 1; i < 50; i++)
                {

                }
                **/



                txtNome.Text = "";
            txtEnd.Text = "";
            txtCidade.Text = "";
            txtEstado.Text = "";
            maskFone.Text = "";
            txtEmail.Text = "";

            btnEditar.Enabled = true;
            btnExcluir.Enabled = true;
            btnGravar.Enabled = false;
        }

        private void btnLimpar_Click(object sender, EventArgs e)
        {
            txtNome.Text = "";
            txtEnd.Text = "";
            txtCidade.Text = "";
            txtEstado.Text = "";
            maskFone.Text = "";
            txtEmail.Text = "";

            btnGravar.Enabled = true;
        }

        private void dgvClientes_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            poc = dgvClientes.CurrentRow.Index;

            txtNome.Text = dgvClientes[1, poc].Value.ToString();
            txtEnd.Text = dgvClientes[2, poc].Value.ToString();
            txtCidade.Text = dgvClientes[3, poc].Value.ToString();
            txtEstado.Text = dgvClientes[4, poc].Value.ToString();
            maskFone.Text = dgvClientes[5, poc].Value.ToString();
            txtEmail.Text = dgvClientes[6, poc].Value.ToString();


            btnGravar.Enabled = false;

        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            dgvClientes[1, poc].Value = txtNome.Text;
            dgvClientes[2, poc].Value = txtEnd.Text;
            dgvClientes[3, poc].Value = txtCidade.Text;
            dgvClientes[4, poc].Value = txtEstado.Text;
            dgvClientes[5, poc].Value = maskFone.Text;
            dgvClientes[6, poc].Value = txtEmail.Text;

            MessageBox.Show("Cliente número: " + i + " Alterado!");
        }

        private void btnExcluir_Click(object sender, EventArgs e)
        {
            if (poc < 0)
                MessageBox.Show("Nenhuma linha selecionada ou não há o que excluir!");
            else
                dgvClientes.Rows.RemoveAt(poc);
        }

        private void frmCadastroClientes_Load(object sender, EventArgs e)
        {

        }

        private void btnNovo_Click(object sender, EventArgs e)
        {
            btnGravar.Enabled = true;
            btnExcluir.Enabled = false;
            btnEditar.Enabled = false;

            txtNome.Text = "";
            txtEnd.Text = "";
            txtCidade.Text = "";
            txtEstado.Text = "";
            maskFone.Text = "";
            txtEmail.Text = "";
        }
    }
}

How can I do that?

1 answer

2


You can read the file every time you make a change to it and upload your combobox with the data you have in it.

string[] lineOfContents = File.ReadAllLines(@"C:\\Users\\willian\\Downloads\\dbClientes.txt");
foreach (var line in lineOfContents)
{
   string[] nomes = line.Split(',');
   comboBox1.Items.Add(nomes[0]);
}
  • I put this code in the load of the form where you will load the comboBox?

  • It worked, that’s right, if you can explain what’s going on in this code that you posted!!

  • Don’t forget to clean the combo before a new read! rsrs

  • @rovannLinhalis because? I’m just adding right.. I just put this code and good

  • 2

    if you come to read the file after a possible change in it... during the execution of the program and not in the form load. then you have to clean the items before, if not, they will be duplicated. If you are reading only once, don’t worry about it

  • @Rovannlinhalis true I just tested this is duplicating all rs. I can put what before the load so I tried: cbClients.Items.Clear() didn’t work

  • Exactly, you will have duplicate data if you do not clean it every time... use the comboBox1.Items.Clear()

  • you are writing twice in the same file ?

  • 1

    puts cbClients.Items.Clear() before the ReadAllLines or of foreach

  • I saw a tutorial that the guy asked to make that code because one was to write the other jumped line

  • @Rovannlinhalis duplicates the same way, I think the error is time to write to txt

  • 1

    is...saw that Voce put the attribute append as true, you may need to review that there

  • 2

    I found where the error was going to change, only need to leave once the using

Show 8 more comments

Browser other questions tagged

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