In mine another forum i had a problem to declare a Form in another Form and they give me the result as follow:
//--------------------------
What I understood from the above is
You have Form1 with a TextBox and a Button
When you click Button it will open Form2
Form2 also has a Button
When you click the Button in Form2 you have toshw the Text in TextBox of Form1.
If my understanding is correct what you have to do is when you are calling Form2 pass the reference of Form1 either in Constructor or a member then you can access the Form1 TextBox(make sure you declare TextBox as Public in Form1)
Form1......
public partial class Form11 : Form
{
public Form11()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form12 f = new Form12(this);
f.ShowDialog();
f.Dispose();
}
}
Form2.....
public partial class Form12 : Form
{
private Form11 _parent;
public Form12(Form11 form)
{
InitializeComponent();
_parent = form;
}
private void button1_Click(object sender, EventArgs e)
{
string halo = _parent.textBox1.Text + " Form2";
MessageBox.Show(halo);
}
}
Hope This Helps
Copying from the IDE: Copy the code from the Visual Studio, see my code above
Now I have a extra Form that need to be add in Form12 called Form13. How can I do that in the same way as the above result ? I was looking the part of public Form12(Form11 form) but i can't find a result.
AlanPosted Aug 9, 2008, 7:42 PM
Form8's constructor need two arguments: a reference to frmTreeView and a reference to Form7.
If you're instantiating Form8 from frmTreeView, it therefore follows that the latter needs a reference to Form7.
If this was passed in frmTreeView's constructor, then you should have stored it in a field. Otherwise, you can get it as follows:
Form7 f7 = (Form7)Application.OpenForms["Form7"];
Form8 f8 = new Form8(this, f7);
Arnold PetronaPosted Aug 9, 2008, 6:46 PM
Form8 f8 = new Form8(this);
if (f8.ShowDialog() == DialogResult)
{
f8.Dispose();
}
the this stands for data of frmTreeView. I think after the this i need ti put the info of Form7 but when i do it it give me a problem. What code i'm missing to add ?
Arnold PetronaPosted Aug 9, 2008, 6:21 PM
private frmTreeView getport1;
private Form7 f7;
public Form8(frmTreeView form, Form7 form7)
{
InitializeComponent();
getport1 = form;
f7 = form7;
}
but from frmTreeView1 Form i need to open Form8 and i have as follow:
Form8 f8 = new Form8(this);
if (f8.ShowDialog() == DialogResult)
{
f8.Dispose();
}
It give me a error message at the red part and is as follow:
No overload for method 'Form8' takes '1' arguments
what can be the problem?
Arnold PetronaPosted Aug 9, 2008, 6:13 PM
public partial class Form13 : Form
{
private Form11 _parent11;
private Form12 _parent12;
public Form13(Form11 form11, Form12 form12)
{
InitializeComponent();
_parent11 = form11;
_parent12 = form12;
}
but i will double check again. Can be i did some stupid mistake.
I will keep you posted
AlanPosted Aug 9, 2008, 5:38 PM
If you needed to access both Form1 and Form2 from Form3 then, using your original approach, you'd need to pass references to the first two forms to Form3's constructor:
public partial class Form12 : Form
{
private Form11 _parent;
public Form12(Form11 form)
{
InitializeComponent();
_parent = form;
}
private void button1_Click(object sender, EventArgs e)
{
string halo = _parent.textBox1.Text + " Form2";
MessageBox.Show(halo);
}
private void button2_Click(object sender, EventArgs e)
{
Form13 f = new Form13(_parent, this);
f.ShowDialog();
f.Dispose();
}
}
public partial class Form13 : Form
{
private Form11 _parent11;
private Form12 _parent12;
public Form13(Form11 form11, Form12 form12)
{
InitializeComponent();
_parent11 = form11;
_parent12 = form12;
}
private void button1_Click(object sender, EventArgs e)
{
string halo = _parent11.textBox1.Text + " Form3";
MessageBox.Show(halo);
_parent12.Text = "Hello from Form 3"; // change Form2's caption
}
}
However, there's no need to pass any parameters using the alternative approach:
// in form3's button_Click
Form11 f11 = (Form11)Application.OpenForms["Form11"];
string halo = f11.Controls["textBox1"].Text + " Form3";
MessageBox.Show(halo);
Form12 f12 = (Form12)Application.OpenForms["Form12"];
f12.Text = "Hello from Form 3";
Arnold PetronaPosted Aug 9, 2008, 4:16 PM
But if I need two information of two different form like Form 1 and Form 2 to have it in Form 3 How can I declare whitout having stack overflow and without working but don't get any data from the other Forms
Thanks for the answer above
AlanPosted Aug 9, 2008, 2:10 PM
I'm assuming that:
1. You have another button on Form12 (button2 say) which, when pressed, opens up Form13.
2. Form13 has a button (button1 say) which, when pressed, accesses the Text property of textBox1 on Form11.
You could then do that like this:
public partial class Form12 : Form
{
private Form11 _parent;
public Form12(Form11 form)
{
InitializeComponent();
_parent = form;
}
private void button1_Click(object sender, EventArgs e)
{
string halo = _parent.textBox1.Text + " Form2";
MessageBox.Show(halo);
}
private void button2_Click(object sender, EventArgs e)
{
Form13 f = new Form13(_parent);
f.ShowDialog();
f.Dispose();
}
}
public partial class Form13 : Form
{
private Form11 _parent;
public Form13(Form11 form)
{
InitializeComponent();
_parent = form;
}
private void button1_Click(object sender, EventArgs e)
{
string halo = _parent.textBox1.Text + " Form3";
MessageBox.Show(halo);
}
}
Incidentally, it's not actually necessary to pass references between forms or to change the accessibility of the textbox to public. Instead you can do it like this:
// in form2's button_Click
Form11 f = (Form11)Application.OpenForms["Form11"];
string halo = f.Controls["textBox1"].Text + " Form2";
MessageBox.Show(halo);
// in form3's button_Click
Form11 f = (Form11)Application.OpenForms["Form11"];
string halo = f.Controls["textBox1"].Text + " Form3";
MessageBox.Show(halo);