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

Asked

Viewed 636 times

2

Hello I’m trying to do something that seems simple but I’m not getting, I want to click on a button add the data of a grid go to another form that is already open. the problem is that I am only able to click on a new form that is not what I want follows 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 the form already opened and not in a new form because I will put other tables that will do the same process if I do this in all I will lose the information.

From now on I thank you all.

3 answers

2


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

If so, you can implement it as follows:

In 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();
    }
}

In the Calling 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;
    }
}
  • Hello Fernando already thank you for the help so I did what you are suggesting more I’m having problems when I put the 'var _frmped = new frmPedidos();' in scope nor can I open my application form already error when I put in the button event nothing happens, I’m still studying what you’ve done to try to solve here also more unsuccessfully so far. Got any more tips?

  • @Jameson, I made a small adjustment to the code. I have tested and worked here.

  • So this way it works in _frmped. Show(); only that it opens another form and what is already open is not filled I think you have to encapsulate the textbox but I don’t know much about how to proceed.If that’s the case

  • With the code you posted, there is no way to know how the opening of frmPedidos. If you’re opening upfrmPedidos before opening Form1, you will have to pass it as parameter to Form1

  • I changed the code to show how you can pass the form opened as a parameter to Form1

  • It worked out with the way you did, the only thing you couldn’t do was to take the data of the Client who had already created the get and set methods in a separate class and to pull it out is right. Very Thanks.

Show 1 more comment

0

It is thus the Form1 frmSlaveConsultClient it is opened from the 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();

    }

form2 which is opened first is so:

 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 put cannot implement yet. is giving errors

  • I think now I understand what you want and I re-read my post. If my answer is correct, I suggest you change your initial question to make it clearer.

0

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

 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();

    }

Browser other questions tagged

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