Lajja Thaker
Lajja Thaker

Reputation: 2041

Passing values from and to Popup forms

I am making windows application.

I have requirement that , I need to open a pop up form from a Button click event , I can open that form through below code

Form2 frm = new  Form2();
frm.ShowDialog();

This Popup form Form2 is data entry form , this will called from Form1. Form2 is data entry form. When I fill fields in Form2 and click on Save button then all the values will be filled in Form1's grid.

And this grid also having Edit and Delete button.

How can I do that ? I have to save data in xml file and retrive from that but I dont want to do that.

Can anyone suggest any another way with sample code?

Upvotes: 1

Views: 6799

Answers (3)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236218

If some data is required by your data entry form, then it's better to pass data via constructor. Other data could be passed via properties, or even methods. Usage of properties is the best way to return data.

using(Form2 fom2 = new Form2(requiredData))
{
    form2.OtherData = optionalData;
    if (form2.ShowDialog() != DialogResult.OK)
        return;
    // read data from dialog form
    grid.DataSource = form2.Data;
    textBox.Text = form2.SomeString;
}

Upvotes: 0

Sinan AKYAZICI
Sinan AKYAZICI

Reputation: 3952

n this way, You can reach Form1 from Form2.

public class Form2
{
    private Form1 _instance;
    public Form2(Form1 instance)
    { 
          _instance = instance;   
    } 

    public void Save()
    {
         _intance.FillMethod();
    }
}

// this = form1 instance
Form2 frm = new  Form2(this);
frm.ShowDialog();

Upvotes: 0

Asif Mushtaq
Asif Mushtaq

Reputation: 13150

You can have some public properties in your form to get and set data.

Form2 frm = new  Form2();
frm.FirstName = "John";
frm.ShowDialog();

string newFirstName = frm.FirstName;

Upvotes: 2

Related Questions