Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C# Programming

C# Programming C# (pronounced C-sharp) is a new object oriented language from Microsoft and is derived from C and C++. It also borrows a lot of concepts from Java too including garbage collection.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-09-2006, 07:10 PM
NeedHelp NeedHelp is offline
Programming God
 
Join Date: May 2006
Posts: 527
Rep Power: 12
NeedHelp is on a distinguished road
Default C# calling parent functions from child form

How do I call parent functions from a child form in C#? What do the modifiers need to be?
__________________
I Need Help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-10-2006, 08:04 PM
Void's Avatar   
Void Void is offline
Programming Expert
 
Join Date: Jun 2006
Posts: 410
Rep Power: 11
Void is on a distinguished road
Default

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
Code:
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
Code:
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-10-2006, 09:56 PM
brackett brackett is offline
Programmer
 
Join Date: May 2006
Posts: 193
Rep Power: 10
brackett is on a distinguished road
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-20-2006, 04:06 PM
NeedHelp NeedHelp is offline
Programming God
 
Join Date: May 2006
Posts: 527
Rep Power: 12
NeedHelp is on a distinguished road
Default

Thanks Void.

Brackett, what do you mean?
__________________
I Need Help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-25-2006, 07:15 PM
Lop's Avatar   
Lop Lop is offline
Speaks fluent binary
 
Join Date: May 2006
Posts: 1,135
Rep Power: 16
Lop is on a distinguished road
Default

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?
__________________
Lop
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 07-26-2006, 12:17 AM
brackett brackett is offline
Programmer
 
Join Date: May 2006
Posts: 193
Rep Power: 10
brackett is on a distinguished road
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 08-10-2006, 09:18 AM
Frantic's Avatar   
Frantic Frantic is offline
Learning Programmer
 
Join Date: May 2006
Posts: 91
Rep Power: 9
Frantic is on a distinguished road
Default

I thought you could just call the parent form by name like

form1.thisfunction

is that not true now?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 08-10-2006, 10:23 AM
hoser2001's Avatar   
hoser2001 hoser2001 is offline
Programmer
 
Join Date: Jul 2006
Posts: 175
Rep Power: 9
hoser2001 is on a distinguished road
Default

Can you use the static modifier for functions to make calling it from child forms easier?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 08-10-2006, 11:53 AM
Crane's Avatar   
Crane Crane is offline
Programming Expert
 
Join Date: Nov 2005
Posts: 399
Rep Power: 13
Crane is on a distinguished road
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Coding a change password form InternetGeek Visual Basic Programming 11 02-16-2008 01:53 PM
Calling a form from another form Void Managed C++ 1 07-01-2006 09:44 AM


All times are GMT -5. The time now is 11:32 PM.

Contest Stats

John ........ 223.00000
dargueta ........ 168.00000
Xav ........ 164.00000
LogicKills ........ 20.00000
gaylo565 ........ 18.00000
WingedPanther ........ 15.00000
|pH| ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 67%

Ads