Search in a form and bring the return to the form you called

Asked

Viewed 526 times

4

in my college project I am doing the product registration ta all right my problem is to load the supplier in the product registration. I created a form frmCartiroProduct and a form frmCadastroFornrcedor in the product registration when I click on search the supplier opens a search screen and I can’t bring back the vendor I selected someone could give me a strength. Windows Form 4 layers

goes down

Product Registration inserir a descrição da imagem aqui

code

  private void pctLocalizaFornecedor_Click(object sender, EventArgs e)
    {
        frmPesquisarFornecedor pesqFornec = new frmPesquisarFornecedor(this);

        pesqFornec.Show();


    }

Search Supplier inserir a descrição da imagem aqui

Code what this commented was what I tried more did not work

   public partial class frmPesquisarFornecedor : Form
{

 //   private int CodFornec;
    //private Form codFornecedor;

    frmCadastroPecas codFornecedor;

    public frmPesquisarFornecedor()
    {
        InitializeComponent();
    }

    public frmPesquisarFornecedor(frmCadastroPecas fm1)
    {
        InitializeComponent();

        codFornecedor = fm1;
        //tbxTextBoxFormB.Text = instanciaDoForm1.tbxTextBoxFormA.Text.ToString();





    }

    //public frmPesquisarFornecedor(int codFornec)
    //{
    //    InitializeComponent();


    //    CodFornec = codFornec;

    //}



    private void frmPesquisarFornecedor_Load(object sender, EventArgs e)
    {
        CarregaGrid();

    }


    #region Carregar o Grid Fornecedor
    private void CarregaGrid()
    {
        try
        {
            IList<FornecedorDTO> listaFornecDTO = new List<FornecedorDTO>();

            listaFornecDTO = new FornecedorModel().CargaFornecedor();// Cria uma estancia do Objeto UsuarioModel
            dgvFornecedor.AutoGenerateColumns = false;// Não vai gerar colunas automaticamente
            dgvFornecedor.DataSource = listaFornecDTO;// carrega o meu grid DataSource ListaUsuarioDTO



        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message);
        }
    }

    #endregion

    private void frmPesquisarFornecedor_FormClosed(object sender, FormClosedEventArgs e)
    {
        frmCadastroPecas obj = new frmCadastroPecas();


      //  obj.codForn = Convert.ToInt32(txtCodigoFornec.Text);
    }

    private void dgvFornecedor_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        int sel = dgvFornecedor.CurrentRow.Index;

        txtCodigoFornec.Text = Convert.ToString(dgvFornecedor["Codigo", sel].Value);
        txtRazaoSocial.Text = Convert.ToString(dgvFornecedor["NomeRazao", sel].Value);
        txtNomeFantasia.Text = Convert.ToString(dgvFornecedor["NomeFantasia", sel].Value);

    }

    private void btnOk_Click(object sender, EventArgs e)
    {
        //PesqFornecDTO objFornec = new PesqFornecDTO();

        //objFornec.Codigo = Convert.ToInt32(txtCodigoFornec.Text);

        //frmCadastroPecas obj = new frmCadastroPecas();
        //obj.codForn = Convert.ToInt32(txtCodigoFornec.Text);

       frmCadastroPecas obj = new frmCadastroPecas();


      //  obj.codForn = Convert.ToInt32(txtCodigoFornec.Text);

    }




}

}

Class of Supplier

public class FornecedorDTO:PessoaDTO
{
    private int codigo;
    private Double vlMinCompra;
    private String telefone2;
    private String contato;
    private int ramal;
    private String userConectado;

    #region Getters e Setters
    public int Codigo
    {
        get { return codigo; }
        set { codigo = value; }
    }

    public String Telefone2
    {
        get { return telefone2; }
        set { telefone2 = value; }
    }
    public String Contato
    {
        get { return contato; }
        set { contato = value; }
    }


    public Double VlMinCompra
    {
        get { return vlMinCompra; }
        set { vlMinCompra = value; }
    }


    public int Ramal
    {
        get { return ramal; }
        set { ramal = value; }
    }


    public String UserConectado
    {
        get { return userConectado; }
        set { userConectado = value; }
    }

on the search screen this right inserir a descrição da imagem aqui

in the tile screen this entry is not in the if (return == Dialogresult.OK) inserir a descrição da imagem aqui

pq. will be ?

1 answer

3


Opá beauty! Try something like this:

//--- frmPesquisarFornecedor ---
// crie propriedades publicas para retorná-las para frmCadastroDeProduto 
public int RetornoCodigo {get;set;} 
public string RetornoRazaoSocial {get;set;}
// é assim por diante...

//--- frmPesquisarFornecedor ---
private void btnOk_Click(object sender,EventArgs e)
{
    this.RetornoCodigo = this.txtCodFornecedorPesquisa.text; // <-- ex: pegando diretamente do textbox
    this.RetornoRazaoSocial = Convert.ToString(dgvFornecedor["NomeRazao", sel].Value); // <-- pegando da grid
    this.DialogResult = DialogResult.OK; // <-- tem que retornar [OK], para passar no if
    this.Close();
}

//--- frmCadastroDeProduto --- 
private void pctLocalizaFornecedor_Click(object sender, EventArgs e)
{
    using (var form = new frmPesquisarFornecedor())
    {
        var retorno = form.ShowDialog(); // <-- aqui está a magia :D
        if (retorno == DialogResult.OK) 
        {
            // os valores vinham como retorno do form assim que ele for fechado
            int codigo = form.RetornoCodigo;
            string razaoSocial = form.RetornoRazaoSocial;

            // assim facilmente pode atribuir á um controle ex:
            this.txtCodigoFornecedor.Text = codigo;
        }   
    }
}
  • It did not work following the error in the question thanks for helping me

  • How strange I’ll check!

  • if I do so it works that if Loko thing (return == Dialogresult.Cancel)

  • I already ran the code, I forgot Dialogresult.OK, sorry!

  • You are the very Thanks Guy will save my life, by the end of the year I’ll have a lot of doubt. I am programming alone in the group and nobody knows and do not even want to learn more I know that I am already learning a lot from this and in the future I can be here helping people as well as you. Thank you very much

  • only one more thing what makes this line using (var form = new frmPesquisarFornecedor())

  • That’s aew fera I’m very happy to have helped, with continue firm in your studies you will go far, thanks!

  • The code inside the Using serves to ensure that the objects are deleted from the memory once the execution of the same, for more information visit this link: https://msdn.microsoft.com/pt-br/library/yh598w02.aspx

Show 3 more comments

Browser other questions tagged

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