How do I call parent functions from a child form in C#? What do the modifiers need to be?
C# calling parent functions from child form
Started by
Guest_NeedHelp_*
, Jul 09 2006 04:10 PM
13 replies to this topic
#1
Guest_NeedHelp_*
Posted 09 July 2006 - 04:10 PM
Guest_NeedHelp_*
|
|
|
#2
Posted 10 July 2006 - 05:04 PM
The modifiers need to be public and this code is a little tricky if you haven't ever done it. I've made a sample program to show how it is done.
The application is two forms and a button on each form. The button on the first form launches form2. The button on form2 calls a function in form1. In order to get the parent you have to pass the form1 instance to form2 on creation (in the constructor). Look below to see how it is done.
Form1.cs
Form2.cs
Should Produce a msgbox with "Parent Function Called"
The application is two forms and a button on each form. The button on the first form launches form2. The button on form2 calls a function in form1. In order to get the parent you have to pass the form1 instance to form2 on creation (in the constructor). Look below to see how it is done.
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace childform
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 tempDialog = new Form2(this);
tempDialog.ShowDialog();
}
public void msgme()
{
MessageBox.Show("Parent Function Called");
}
}
}
Form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace childform
{
public partial class Form2 : Form
{
private Form1 m_parent;
public Form2(Form1 frm1)
{
InitializeComponent();
m_parent = frm1;
}
private void button1_Click(object sender, EventArgs e)
{
m_parent.msgme();
}
}
}
Should Produce a msgbox with "Parent Function Called"
Void
#3
Posted 10 July 2006 - 06:56 PM
Just a quick note that you could also mark the methods as "internal" (or "Friend" in VB.NET) to allow access only to classes in the same assembly. That cuts down your public API, which means less maintenance.
#4
Guest_NeedHelp_*
Posted 20 July 2006 - 01:06 PM
Guest_NeedHelp_*
Thanks Void.
Brackett, what do you mean?
Brackett, what do you mean?
#5
Posted 25 July 2006 - 04:15 PM
So basically, all you need to do is pass your parent form to the constructor of the child form. Fairly easy, probably wouldn't of thought of that though.
What do you mean brackett?
What do you mean brackett?
#6
Posted 25 July 2006 - 09:17 PM
Without getting into too much of a digression, I was simply pointing out that instead of making your method (in the example, Form1.MsgMe) "public", it'd be better to make it "internal" to the assembly.
By making it "public", you expose it to be called and used by any code that links against it - code which you may not control. That means you should be worried about versioning, which means you can't change the signature of that method without client code changing.
By making it "internal", you only expose it to other classes in the same assembly - code that you control. Now, you can change the method, and client code, without repercussions.
Of course, Reflection can get around the "internal" (or "private", for that matter) restrictions, but folks who use Reflection in that manner expect to be broken in future versions anyway.
By making it "public", you expose it to be called and used by any code that links against it - code which you may not control. That means you should be worried about versioning, which means you can't change the signature of that method without client code changing.
By making it "internal", you only expose it to other classes in the same assembly - code that you control. Now, you can change the method, and client code, without repercussions.
Of course, Reflection can get around the "internal" (or "private", for that matter) restrictions, but folks who use Reflection in that manner expect to be broken in future versions anyway.
#7
Posted 10 August 2006 - 06:18 AM
I thought you could just call the parent form by name like
form1.thisfunction
is that not true now?
form1.thisfunction
is that not true now?
#8
Posted 10 August 2006 - 07:23 AM
Can you use the static modifier for functions to make calling it from child forms easier?
#9
Posted 10 August 2006 - 08:53 AM
Not sure about your question hoser2001 but you can call it like that frantic if you create a new form1 from your new form. It will not be the parent form1 but a new one.
#10
Posted 29 December 2009 - 02:56 PM
Thanks Void,
I'm a bit rusty and had forgotten how to do this. This is a gift that keeps on giving.
I'm a bit rusty and had forgotten how to do this. This is a gift that keeps on giving.
#11
Posted 08 January 2010 - 06:00 AM
I wondered that myself, thanks for answering!
#12
Posted 23 June 2010 - 01:56 AM
Hy. I want to start Form1 function from Form2,but i dont know how....(the function start and run on form1 and not on form2 and i wannt to take parameters to the function from form2 :))
Can someone help me?
Can someone help me?


Sign In
Create Account

Back to top










