Mayank Jani

Mayank Jani

  • 1.7k
  • 477
  • 67.1k

Pass value from one form to another form and refresh.

Jun 17 2017 2:41 AM
Greetings of the day...
I am trying to develope a small programme to pass value of one form to another form in C# Windows application. I have one form (the first form) contains 3 textboxes and a command button. when i click the command button,  it opens the second form that also contains 3 textboxes and a command button. now when i fill data (like name, age and city) and click on the command button, the second form closes and the data shows in the first form.
my problem is i have to reload the form to display the data in the first form. this way the first form opens twice (i.e. first when programme runs and second when reload to display the values). i found a solution that when i click on first form's command button (to open second form), the first form get closed. but i want to do it without closing thee first form and just REFRESH the first form when second form is closed to transfer the data to first form.
please see my code...
//Code in the First Form
private void btnAdd_Click(object sender, EventArgs e)   //to open the second form
Form frmPassValuesSub = new frmPassValuesSub();

private void frmPassValues_Load(object sender, EventArgs e)   //to reload to display the values
txtReturnName.Text = frmPassValuesSub.SetValueForName;
txtReturnSex.Text = frmPassValuesSub.SetValueForSex;
txtReturnCity.Text = frmPassValuesSub.SetValueForCity; 
//Code in the Second Form
public static string SetValueForName = "";   //Declaing global variables to store values.
public static string SetValueForSex = "";
public static string SetValueForCity = "";

public frmPassValuesSub()

private void btnSubmit_Click(object sender, EventArgs e)   //Second form to pass values
SetValueForName = txtName.Text;
SetValueForSex = txtSex.Text;
SetValueForCity = cmbCity.Text;

Form frmPassValues = new frmPassValues();

So, i have to close and reopen the first form to avoid the same to open twice (reload to show the data). please suggest me how i can REFRESH the form without closing it.
thank you...

Answers (18)