How do I pass data from child of child form to parent form in C#?
-
I am new to c#. I have the following in my project in windows forms: Form1 with button and DataGridView. Form2 with button. Form3 with button and 3 textBoxes. In form1, I click buttonOpenForm2 form2 pops up. Then in form2 I click buttonOpenForm3 form3 pops up which has 3 text boxes and button. Now the 3 forms are open. now in form3, I enter values in textBox1, textBox2 and textBox3 and when click buttonAddRow ( from form3) I want these values to be inserted into the DataGRidView in Form1. My question is: How can I add a row into DataGridView in Form1 ( parent) from form3 (child of child form) WITHOUT closing form2 and form3? I mean I want to pass the data while form2 and form3 are still open. Please help me. Thank you
Form1:
public partial class Form1 : Form
{public Form1() { InitializeComponent(); } private void buttonOpenForm2 \_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.Show(); }
}
Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}private void buttonOpenForm3 \_Click(object sender, EventArgs e) { Form3 frm3 = new Form3(); frm3.Show(); }
}
Form3:
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}private void buttonAddRow \_Click(object sender, EventArgs e) { //What to write here to insert the 3 textboxes values into DataGridView? }
}
-
I am new to c#. I have the following in my project in windows forms: Form1 with button and DataGridView. Form2 with button. Form3 with button and 3 textBoxes. In form1, I click buttonOpenForm2 form2 pops up. Then in form2 I click buttonOpenForm3 form3 pops up which has 3 text boxes and button. Now the 3 forms are open. now in form3, I enter values in textBox1, textBox2 and textBox3 and when click buttonAddRow ( from form3) I want these values to be inserted into the DataGRidView in Form1. My question is: How can I add a row into DataGridView in Form1 ( parent) from form3 (child of child form) WITHOUT closing form2 and form3? I mean I want to pass the data while form2 and form3 are still open. Please help me. Thank you
Form1:
public partial class Form1 : Form
{public Form1() { InitializeComponent(); } private void buttonOpenForm2 \_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.Show(); }
}
Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}private void buttonOpenForm3 \_Click(object sender, EventArgs e) { Form3 frm3 = new Form3(); frm3.Show(); }
}
Form3:
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}private void buttonAddRow \_Click(object sender, EventArgs e) { //What to write here to insert the 3 textboxes values into DataGridView? }
}
Use events and properties: Form3 signals to it's "parent" Form2, which gathers the data and signals an event to it's parent, Form1. Sound complex, but it's really simple! See here: Transferring information between two forms, Part 2: Child to Parent[^]
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
I am new to c#. I have the following in my project in windows forms: Form1 with button and DataGridView. Form2 with button. Form3 with button and 3 textBoxes. In form1, I click buttonOpenForm2 form2 pops up. Then in form2 I click buttonOpenForm3 form3 pops up which has 3 text boxes and button. Now the 3 forms are open. now in form3, I enter values in textBox1, textBox2 and textBox3 and when click buttonAddRow ( from form3) I want these values to be inserted into the DataGRidView in Form1. My question is: How can I add a row into DataGridView in Form1 ( parent) from form3 (child of child form) WITHOUT closing form2 and form3? I mean I want to pass the data while form2 and form3 are still open. Please help me. Thank you
Form1:
public partial class Form1 : Form
{public Form1() { InitializeComponent(); } private void buttonOpenForm2 \_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.Show(); }
}
Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}private void buttonOpenForm3 \_Click(object sender, EventArgs e) { Form3 frm3 = new Form3(); frm3.Show(); }
}
Form3:
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}private void buttonAddRow \_Click(object sender, EventArgs e) { //What to write here to insert the 3 textboxes values into DataGridView? }
}
What was wrong with the answer you got on November 6th. when you asked the same question: [^] ? Why use three Forms when you can use one Form and UserControls ?
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
-
Use events and properties: Form3 signals to it's "parent" Form2, which gathers the data and signals an event to it's parent, Form1. Sound complex, but it's really simple! See here: Transferring information between two forms, Part 2: Child to Parent[^]
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
What was wrong with the answer you got on November 6th. when you asked the same question: [^] ? Why use three Forms when you can use one Form and UserControls ?
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
-
Hi, thank you for the help, I will have to study the properties and events then i will test the code written in the answer. thank you
You're welcome!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
I am new to c#. I have the following in my project in windows forms: Form1 with button and DataGridView. Form2 with button. Form3 with button and 3 textBoxes. In form1, I click buttonOpenForm2 form2 pops up. Then in form2 I click buttonOpenForm3 form3 pops up which has 3 text boxes and button. Now the 3 forms are open. now in form3, I enter values in textBox1, textBox2 and textBox3 and when click buttonAddRow ( from form3) I want these values to be inserted into the DataGRidView in Form1. My question is: How can I add a row into DataGridView in Form1 ( parent) from form3 (child of child form) WITHOUT closing form2 and form3? I mean I want to pass the data while form2 and form3 are still open. Please help me. Thank you
Form1:
public partial class Form1 : Form
{public Form1() { InitializeComponent(); } private void buttonOpenForm2 \_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.Show(); }
}
Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}private void buttonOpenForm3 \_Click(object sender, EventArgs e) { Form3 frm3 = new Form3(); frm3.Show(); }
}
Form3:
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}private void buttonAddRow \_Click(object sender, EventArgs e) { //What to write here to insert the 3 textboxes values into DataGridView? }
}
Build some business logic, i.e. class(es) for storing the text entered form3 and pass it/them to form1 in buttonAddRow_Click or make something like this
public struct EnteredText
{
public string t1, t2, t3;
};public partial class Form3 : Form
{
public delegate void ButtonClick(EnteredText data);
public event ButtonClick OnClick;
...
private void buttonAddRow _Click(object sender, EventArgs e)
{
EnteredText args;
args.t1 = textbox1.Text; args.t2 = ...
if (OnClick != null)
OnClick(args);
}
}Means you copy the text from the boxes into the struct and fire the event OnClick. Somewhere in Form2 you should do something like this:
Form3 f3 = new Form3();
f3.OnClick += f3_OnClick;
f3.Show();
}
private void f3_OnClick(EnteredText txt)
{
// do something with the text or refire a new event.
}