thinzar
thinzar

Reputation: 1550

Form Refreshing Problem in c#

I have two form form A and from B . When user click button on form A , Form B will apperar but Form A is already open .User can set data in form B. When user click add Button in Form B ,the data will pass to form A and textbox in form A will set data with pass data. I received passed data but Form A's textbox not fill with data.How could I do that ?

In Form B add button clik

private void btnAdd_Click(object sender, EventArgs e)
        {

            EmployeeAddressEntity empAdd = new EmployeeAddressEntity();
            empAdd = AddEmployee();

            this.Close();
            NewEmployee emp = new NewEmployee();
            emp.FillAddressGrid(empAdd);    

        }

In Form A

public void FillAddressGrid(EmployeeAddressEntity emp)
        {
            txtAddressName.Text = emp.name;

            dgvAddress.Rows.Add(emp.type,emp.name,emp.homephone,emp.fax,emp.email,emp.address,emp.country_id);
            //int a = dgvAddress.Rows.Count;
            dgvAddress.EndEdit();
            dgvAddress.Refresh();
            dgvAddress.Parent.Refresh();

            this.Refresh();
        }

Upvotes: 1

Views: 859

Answers (3)

Cos Callis
Cos Callis

Reputation: 5084

When you call NewEmployee emp = new NewEmployee(); in form b you are creating a reference to a NEW instance of FORM A (which is 'NewEmployee', right?) but you never actually SHOW this instance. When you invoke emp.FillAddressGrid(empAdd); you are acting on a hidden instance NOT the one which you can see.

When Your create an your instance of form B you need to pass a reference to form B like:

    FormB formB = new FormB();
    formB.Owner = this;
    fromB.show();

This will give you the ability to later call:

((NewEmployee)Owner).FillAddressGrid(empAdd);

from Form B.

Upvotes: 2

David Anderson
David Anderson

Reputation: 13680

In FormB, define a private field that stores the reference to FormA, and create a custom constructor to pass the reference.

public FormB(FormA form) {
    this.m_FormA = form;
} 

private FormA m_FormA;

When you show FormB from FormA, pass the reference.

using (FormB form = new FormB(this)) {
    form.Show();
}

When you are ready to update FormA, call your member function.

this.m_FormA.FillEmployeeGrid(empAdd);

Once you have all this working, you should not have a need to call Refresh() to update the UI unless something is going to be blocking the UI thread (in which you may want to do some asynchronous anyway).

Upvotes: 3

SAJEER
SAJEER

Reputation: 1

so u getting data from form B to form A, Store recieved data in one variable and assign that variable to the textbox. like, textbox1.text=v.tostring();

Upvotes: -1

Related Questions