Jump to content

C# calling parent functions from child form

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
13 replies to this topic

#1
Guest_NeedHelp_*

Guest_NeedHelp_*
  • Guests
How do I call parent functions from a child form in C#? What do the modifiers need to be?

#2
Void

Void

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 411 posts
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

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
brackett

brackett

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts
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_*

Guest_NeedHelp_*
  • Guests
Thanks Void.

Brackett, what do you mean?

#5
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts
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?

#6
brackett

brackett

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts
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.

#7
Frantic

Frantic

    Learning Programmer

  • Members
  • PipPipPip
  • 91 posts
I thought you could just call the parent form by name like

form1.thisfunction

is that not true now?

#8
hoser2001

hoser2001

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
Can you use the static modifier for functions to make calling it from child forms easier?

#9
Crane

Crane

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 398 posts
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
Grillerinthemist

Grillerinthemist

    Newbie

  • Members
  • Pip
  • 1 posts
Thanks Void,

I'm a bit rusty and had forgotten how to do this. This is a gift that keeps on giving.

#11
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
I wondered that myself, thanks for answering!

#12
yoda174

yoda174

    Newbie

  • Members
  • PipPip
  • 25 posts
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?