Load grid information into textbox in a form that is already open c # windows form

1

Hello, I'm trying to do something that looks simple but I'm not getting it, I want to click a button to add the data from a grid go to another form that is already open. The problem is that I am only able to load a new form. This is not what I want. It follows the code for help:

Add Form1 button event

  private void btnAdicionar_Click(object sender, EventArgs e)
    {

        frmPedidos frmped = new frmPedidos(CODIGO, NOME, TELEFONE, ENDERECO); 

        CODIGO = int.Parse(dgvConsultaCliente.CurrentRow.Cells[0].Value.ToString());
        NOME = dgvConsultaCliente.CurrentRow.Cells[1].Value.ToString();
        TELEFONE = dgvConsultaCliente.CurrentRow.Cells[2].Value.ToString();
        ENDERECO = dgvConsultaCliente.CurrentRow.Cells[3].Value.ToString();

        frmped.Show();// aqui que ele carrega outro form tanto faz se usar ShowDialog ou Show mesmo
    }

Form 2

 public frmPedidos(int codigo, string nome, string telefone, string endereco)
    {
        InitializeComponent();
        txtCodCliente.Text = codigo.ToString();
        txtNomeCliente.Text = nome;
        txtTelefoneCliente.Text = telefone; 
        txtEndCliente.Text = endereco;


    }

I want to fill in form2 already open and not in a new form because I will put more other tables that will do the same process if I do this in all I will lose the information.

Thanks in advance.

    
asked by anonymous 29.10.2017 / 18:40

3 answers

1

If I understand correctly, you want to open a form to do a customer search and return the selected customer to the calling form.

If this is the case, you can implement it as follows:

On the Search Form:

public partial class frmSlaveConsultaCliente : Form
{
    //propriedades com os dados do cliente selecionado
    public int Codigo {get; private set;}
    public string Nome {get; private set;}
    public string Telefone {get; private set;}
    public string Endereco {get; private set;}

    public frmSlaveConsultaCliente()
    {
        InitializeComponent();
    }

    void btnAdicionar_Click(object sender, EventArgs e)
    {
        this.Codigo = int.Parse(dgvConsultaCliente.CurrentRow.Cells[0].Value.ToString());
        this.Nome = dgvConsultaCliente.CurrentRow.Cells[1].Value.ToString();
        this.Telefone =  dgvConsultaCliente.CurrentRow.Cells[2].Value.ToString();
        this.Endereco =  dgvConsultaCliente.CurrentRow.Cells[3].Value.ToString();

        this.DialogResult = DialogResult.OK; // para indicar ao Form chamador que o usuário adicionou um cliente
        this.Close();
    }
}

On Caller Form:

public partial class frmPedidos : Form
{
    public frmPedidos()
    {
        InitializeComponent();
    }


    private void btnProcSabor_Click(object sender, EventArgs e)
    {
        var frmSlaveProd = new frmSlaveConsultaCliente();

        if (frmSlaveProd.ShowDialog() != DialogResult.OK)
            return;

        txtCodCliente.Text = frmSlaveProd.Codigo.ToString();
        txtNomeCliente.Text = frmSlaveProd.Nome;
        txtTelefoneCliente.Text = frmSlaveProd.Telefone; 
        txtEndCliente.Text = frmSlaveProd.Endereco;
    }
}
    
29.10.2017 / 22:26
0

It is thus the form1 frmSlaveConsultaCliente it is opened from form2 frmPedidos

 public partial class frmSlaveConsultaCliente : Form
{
  //  frmPedidos _frmped = new frmPedidos();
    public frmSlaveConsultaCliente()
    {
        InitializeComponent();

    }
    ConexaoClienteDataContext cc = new ConexaoClienteDataContext();


    public  int CODIGO;
    public  string NOME;

    public string TELEFONE;

    public  string ENDERECO;

    private void frmSlaveConsultaCliente_Load(object sender, EventArgs e)
    {

        ListarCliente();


    }

    public void ListarCliente()
    {
        dgvConsultaCliente.DataSource = cc.pListarCliente();
    }

    private void btnAdicionar_Click(object sender, EventArgs e)
    {


        CODIGO = int.Parse(dgvConsultaCliente.CurrentRow.Cells[0].Value.ToString());
        NOME = dgvConsultaCliente.CurrentRow.Cells[1].Value.ToString();
        TELEFONE = dgvConsultaCliente.CurrentRow.Cells[2].Value.ToString();
        ENDERECO = dgvConsultaCliente.CurrentRow.Cells[3].Value.ToString();

        //  _frmped.PreencherCampos(CODIGO, NOME, TELEFONE, ENDERECO);
        frmPedidos.TextBoxcontaine txt = new frmPedidos.TextBoxcontaine();

    }

the form2 that is opened first looks like this:

 public partial class frmPedidos : Form
{


    public frmPedidos()
    {
        InitializeComponent();



    }


    public void PreencherCampos(int codigo, string nome, string telefone, string endereco)
    {
        txtCodCliente.Text = codigo.ToString();
        txtNomeCliente.Text = nome;
       txtTelefoneCliente.Text = telefone;
        txtEndCliente.Text = endereco;


    }


   // frmSlaveConsultaCliente frmClid = new frmSlaveConsultaCliente();


    private void textBox8_TextChanged(object sender, EventArgs e)
    {

    }

    private void label5_Click(object sender, EventArgs e)
    {

    }

    private void btnProcSabor_Click(object sender, EventArgs e)
    {
        frmSlaveConsultaProduto frmSlaveProd = new frmSlaveConsultaProduto();
        frmSlaveProd.Show();
    }

The code you have placed can not be implemented yet. is giving errors

    
30.10.2017 / 01:13
0

If you uncomment the lines below it works, but I believe you need to call the Refresh event of your form to load the data on the screen, try:

 public partial class frmSlaveConsultaCliente : Form
{
    frmPedidos _frmped = new frmPedidos();
    public frmSlaveConsultaCliente()
    {
        InitializeComponent();

    }
    ConexaoClienteDataContext cc = new ConexaoClienteDataContext();


    public  int CODIGO;
    public  string NOME;

    public string TELEFONE;

    public  string ENDERECO;

    private void frmSlaveConsultaCliente_Load(object sender, EventArgs e)
    {

        ListarCliente();


    }

    public void ListarCliente()
    {
        dgvConsultaCliente.DataSource = cc.pListarCliente();
    }

    private void btnAdicionar_Click(object sender, EventArgs e)
    {


        CODIGO = int.Parse(dgvConsultaCliente.CurrentRow.Cells[0].Value.ToString());
        NOME = dgvConsultaCliente.CurrentRow.Cells[1].Value.ToString();
        TELEFONE = dgvConsultaCliente.CurrentRow.Cells[2].Value.ToString();
        ENDERECO = dgvConsultaCliente.CurrentRow.Cells[3].Value.ToString();

        _frmped.PreencherCampos(CODIGO, NOME, TELEFONE, ENDERECO);
        _frmped.Refresh();

    }
    
30.10.2017 / 11:59