call an opener form method
-
hi guys, i have a form that needs to call a function on his opener form. How can i do this? ex: form1 opens form2, and form2 needs to call a method in form1. thanks a lot!
Make the method public, and pass a reference to itself to the second form (in the second form's constructor).
*** In Form1 ***
Form2 form2 = new Form2(this);*** In Form2 ***
public Form2 (Form1 form1) : this()
{
_form1 = form1;
}private Form1 _form1;
-
Make the method public, and pass a reference to itself to the second form (in the second form's constructor).
*** In Form1 ***
Form2 form2 = new Form2(this);*** In Form2 ***
public Form2 (Form1 form1) : this()
{
_form1 = form1;
}private Form1 _form1;
-
This works for me. Two forms, one with a button and textbox (Form1), the other just a button (Form2).
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void button1\_Click(object sender, EventArgs e) { Form2 form2 = new Form2(this); form2.Show(); } public void Change(string text) { textBox1.Text = text; }
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}public Form2(Form1 form1) : this() { \_form1 = form1; } private Form1 \_form1; private void button1\_Click(object sender, EventArgs e) { \_form1.Change("Put some text in the TextBox"); }
}
-
hi guys, i have a form that needs to call a function on his opener form. How can i do this? ex: form1 opens form2, and form2 needs to call a method in form1. thanks a lot!
you can do it something like:
using(Form2 frm2 = new Form2()) { frm2.myVoid(); }
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com
-
This works for me. Two forms, one with a button and textbox (Form1), the other just a button (Form2).
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void button1\_Click(object sender, EventArgs e) { Form2 form2 = new Form2(this); form2.Show(); } public void Change(string text) { textBox1.Text = text; }
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}public Form2(Form1 form1) : this() { \_form1 = form1; } private Form1 \_form1; private void button1\_Click(object sender, EventArgs e) { \_form1.Change("Put some text in the TextBox"); }
}
-
ok, almost there... i got only one more problem... i need to pass 2 param to my form2. ex: From2 form = new Form2(id, this);
Just change the constructor... Simple.
public Form2(int id, Form1 form1) : this()
{
_id = id;
_form1 = form1;
}private int _id;
private Form1 _form1; -
This works for me. Two forms, one with a button and textbox (Form1), the other just a button (Form2).
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void button1\_Click(object sender, EventArgs e) { Form2 form2 = new Form2(this); form2.Show(); } public void Change(string text) { textBox1.Text = text; }
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}public Form2(Form1 form1) : this() { \_form1 = form1; } private Form1 \_form1; private void button1\_Click(object sender, EventArgs e) { \_form1.Change("Put some text in the TextBox"); }
}
-
The this() call makes the constuctor call the lower-level constructor overload. In this example,
Form2(Form1 form1) : this()
causes it to callpublic Form2()
during its execution, which thereby causes it to callInitializeComponent();
(and anything else insideForm2()
). If you don't have a parameter-less constructor overload of course, it will fail. ;)public Form2()
{
InitializeComponent();
}public Form2(Form1 form1) : this()
{
_form1 = form1;
} -
hi guys, i have a form that needs to call a function on his opener form. How can i do this? ex: form1 opens form2, and form2 needs to call a method in form1. thanks a lot!
Either do the way others have suggested or follow on of the following approach: 1. Create a class called Common or something similar and place the methods used by more than one forms there. Then all the forms can call methods in this class. Or 2. Have a base class that contains common methods (possibly marked virtual) and all forms in your application inherit from that class.
"Your code will never work, Luc's always will.", Richard MacCutchan[^]
-
you can do it something like:
using(Form2 frm2 = new Form2()) { frm2.myVoid(); }
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com
your answer is not clear at all. If this is a third form you are creating, it is wrong as it would not share anything with the original Form it is trying to interact with. :|
Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum
Season's Greetings to all CPians.
-
hi guys, i have a form that needs to call a function on his opener form. How can i do this? ex: form1 opens form2, and form2 needs to call a method in form1. thanks a lot!
I never like solutions that include a child knowing about its parent. I question whether the UI forms should be communicating directly in the first place and if some re-architecturing needs to take place on that design. But suffice to say, if the forms do need to communicate, I would much rather see it done where Form2 raises an event for which Form1 creates a handler when creating Form2. That way, at least the child still knows nothing about the parent. if( eventToCall != null ) eventToCall();
I wasn't, now I am, then I won't be anymore.
-
I never like solutions that include a child knowing about its parent. I question whether the UI forms should be communicating directly in the first place and if some re-architecturing needs to take place on that design. But suffice to say, if the forms do need to communicate, I would much rather see it done where Form2 raises an event for which Form1 creates a handler when creating Form2. That way, at least the child still knows nothing about the parent. if( eventToCall != null ) eventToCall();
I wasn't, now I am, then I won't be anymore.
With events instead, it could look something like this. (Probably better to pass the message in an event argument, though.)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
form2.Clicked += new EventHandler(form2_Clicked);
}private void form2\_Clicked(object sender, EventArgs e) { textBox1.Text = form2.Message; } private void button1\_Click(object sender, EventArgs e) { form2.Show(); } private Form2 form2 = new Form2();
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}private void button1\_Click(object sender, EventArgs e) { Message = "Here is some text"; if (Clicked != null) Clicked(sender, e); } public string Message = string.Empty; public event EventHandler Clicked;
}
[Edit] Public to private on Form2.
modified on Wednesday, January 5, 2011 1:31 PM
-
With events instead, it could look something like this. (Probably better to pass the message in an event argument, though.)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
form2.Clicked += new EventHandler(form2_Clicked);
}private void form2\_Clicked(object sender, EventArgs e) { textBox1.Text = form2.Message; } private void button1\_Click(object sender, EventArgs e) { form2.Show(); } private Form2 form2 = new Form2();
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}private void button1\_Click(object sender, EventArgs e) { Message = "Here is some text"; if (Clicked != null) Clicked(sender, e); } public string Message = string.Empty; public event EventHandler Clicked;
}
[Edit] Public to private on Form2.
modified on Wednesday, January 5, 2011 1:31 PM
In theory correct, but there may be a few issues.
public Form2 form2 = new Form2();
should probably be made a private member unless it it required by something outside this form.(That would likely not be a good idea) Other than that, I think it is pretty close.I wasn't, now I am, then I won't be anymore.
-
In theory correct, but there may be a few issues.
public Form2 form2 = new Form2();
should probably be made a private member unless it it required by something outside this form.(That would likely not be a good idea) Other than that, I think it is pretty close.I wasn't, now I am, then I won't be anymore.
Did I say public? I meant private... :-O :-D
-
hi guys, i have a form that needs to call a function on his opener form. How can i do this? ex: form1 opens form2, and form2 needs to call a method in form1. thanks a lot!
Might as well give the "event" version that passes the string as an event arg...
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
form2.Clicked += new Form2.StringEventHandler(form2_Clicked);
}private void form2\_Clicked(string message) { textBox1.Text = message; } private void button1\_Click(object sender, EventArgs e) { form2.Show(); } private Form2 form2 = new Form2();
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}private void button1\_Click(object sender, EventArgs e) { if (Clicked != null) Clicked("Here is some text"); } public event StringEventHandler Clicked; public delegate void StringEventHandler(string message);
}
-
Did I say public? I meant private... :-O :-D
Hide your members whenever possible... Showing them almost always leads to embarrassment and ridicule... :)
I wasn't, now I am, then I won't be anymore.