Click here to Skip to main content
15,882,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi every
i have two forms in form A i have a button with textbox which open another form with grid view where i add some values and get sum of these values in textbox i need to back textbox sum in form B to textbox In form A ?? how it can be done ? i use c# .
i try apiece of code but i receive this value in form A

What I have tried:

this.ReturnValue1 = txttotal.Text;
// this.ReturnValue2 = DateTime.Now.ToString(); //example
this.DialogResult = DialogResult.OK;
this.Close();
Posted
Updated 31-Jan-23 1:42am

Yes - there are a couple of ways, depending on when you want it passed back.
If you use ShowDialog to display the second form, then add a property to the form and read it back after the form has closed.
If you want to update the "main" form without closing the child, use an event: Transferring information between two forms, Part 2: Child to Parent[^]
 
Share this answer
 
Comments
MR.alaa 27-Sep-16 6:04am    
it doesn't work with me !! because i have data which i pass it to form 2 and i need to receive a new different data superpose it need in event form close and then pass the values to the first form i declare public variable but how can refresh the form on the child close with the new data ??
OriginalGriff 27-Sep-16 6:16am    
Don't declare public variables! Bad idea...
How are you showing the new form? Are you calling Show or ShowDialog?
try this, refer in line comments

Form2
C#
public partial class Form2 : Form
  {
      public string SumValue { get; set; } // create a property in form 2
      private void textBox1_TextChanged(object sender, EventArgs e) // use TextChanged event from properties window
      {
          SumValue = textBox1.Text;
      }


Form1

C#
public partial class Form1 : Form
   {
       private void button1_Click(object sender, EventArgs e)
       {
           Form2 objForm2 = new Form2();
           objForm2.ShowDialog();
           this.textBox1.Text = objForm2.SumValue; // read the value from form2

       }
 
Share this answer
 
Comments
MR.alaa 27-Sep-16 6:17am    
in form 1 which button click nothing to click it should receive sumvalue of form2 on form2 closing or as form 1 refresh
Karthik_Mahalingam 27-Sep-16 6:38am    
ok wait, i willupdate the solution.
MR.alaa 27-Sep-16 6:44am    
okay
Karthik_Mahalingam 27-Sep-16 6:57am    
try this

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{

foreach (Form f in Application.OpenForms)
{
if (f.Name == "Form1")
{
(f as Form1).textBox1.Text = this.textBox1.Text;
}
}

}

note: make the textbox in form1 as public modifier
MR.alaa 27-Sep-16 7:33am    
Thanks worked successfully
in each text change the form opens multiple times. how to avoid this?
 
Share this answer
 
Comments
Richard Deeming 31-Jan-23 8:17am    
Your question is not a "solution" to someone else's question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900