Search code examples
c#.netwinformsentity-frameworkentity

How to call Form in a function


ı have two forms when cariGuide.cs forms cell double click function perform ı want to go Form1.cs or Form2.cs if previous form form1 or form2 ?

Here is my code block :

public void GoBackMain(Form frm) //
        {
            if (dgvCariRehber.CurrentRow.Index != -1)
            {
                cariModel.Id = Convert.ToInt32(dgvCariRehber.CurrentRow.Cells["Id"].Value);
                using (Entities db = new Entities())
                {
                    cariModel = db.xcaSabits.Where(x => x.Id == cariModel.Id).FirstOrDefault();
                    frm.lblCariID.Text = cariModel.Id.ToString(); // cariSabitte güncelleme için id yi gönder.
                    frm.txtCariKodu.Text = cariModel.cariKodu;

                    frm.Show();
                    this.Hide();
                }
            }
        }

My question is how to catch and understand data of previous form and send function (Form frm)


Solution

  • Finally ı found it !!

    First you have to declare your main form ;

            frmMain originalForm;
            public frmCariRehber(frmMain incomingForm)
            {
                originalForm = incomingForm;
                InitializeComponent();
            }
    

    this form is your main form which is not closing and you return the coming FORM and manage FORM with this.

    After that ;

    public void GoBackMain()
            {
                if (dgvCariRehber.CurrentRow.Index != -1)
                {
                    cariModel.Id = Convert.ToInt32(dgvCariRehber.CurrentRow.Cells["Id"].Value);
                    using (fastCellDb db = new fastCellDb())
                    {
                        cariModel = db.xcaSabits.Where(x => x.Id == cariModel.Id).FirstOrDefault();
                        originalForm.CariID = cariModel.Id;
                        originalForm.CariKodu = cariModel.cariKodu;
                        originalForm.CariAdi = cariModel.cariIsim;
                        originalForm.CariPopulate();
                        this.Close();
                    }
                }
            }
    

    Here is my Datagridview cell click function GoBackMain().

    Talk about MainForm.cs

    public string StokKodu,CariKodu,CariAdi;
    public decimal StokFiyati;
    public int StokID,CariID;
    

    you have to declare variables you return opening form.

    and finally get the values MainForm.cs !!

         internal void StokPopulate()
            {
                txtStokKodu.Text = StokKodu;
                lblStokID.Text = StokID.ToString();
            }
         internal void CariPopulate()
            {
                txtCariKodu.Text = CariKodu;
                lblCariID.Text = CariID.ToString();
                lblCariGetir.Text = CariAdi;
            }