2
This is what I need to do:
- Registering a customer on the form.
Save the
nome
of clients that are registered in a txt file on the computer, example:- John
- Marcos
- Willian
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?
I put this code in the load of the form where you will load the
comboBox
?– WSS
It worked, that’s right, if you can explain what’s going on in this code that you posted!!
– WSS
Don’t forget to clean the combo before a new read! rsrs
– Rovann Linhalis
@rovannLinhalis because? I’m just adding right.. I just put this code and good
– WSS
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
– Rovann Linhalis
@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– WSS
Exactly, you will have duplicate data if you do not clean it every time... use the comboBox1.Items.Clear()
– Marco Souza
you are writing twice in the same file ?
– Marco Souza
puts
cbClients.Items.Clear()
before theReadAllLines
or offoreach
– Rovann Linhalis
I saw a tutorial that the guy asked to make that code because one was to write the other jumped line
– WSS
@Rovannlinhalis duplicates the same way, I think the error is time to write to txt
– WSS
is...saw that Voce put the attribute
append
as true, you may need to review that there– Rovann Linhalis
I found where the error was going to change, only need to leave once the
using
– WSS