Click here to Skip to main content
15,905,683 members
Home / Discussions / C#
   

C#

 
AnswerRe: Any alternative Pin
DaveyM6921-Jan-09 7:34
professionalDaveyM6921-Jan-09 7:34 
AnswerRe: Any alternative Pin
PIEBALDconsult21-Jan-09 7:39
mvePIEBALDconsult21-Jan-09 7:39 
AnswerRe: Any alternative Pin
Jon Rista21-Jan-09 10:27
Jon Rista21-Jan-09 10:27 
Questionparent child communication Pin
bfis10813721-Jan-09 6:35
bfis10813721-Jan-09 6:35 
AnswerRe: parent child communication Pin
PIEBALDconsult21-Jan-09 6:50
mvePIEBALDconsult21-Jan-09 6:50 
GeneralRe: parent child communication Pin
bfis10813721-Jan-09 7:09
bfis10813721-Jan-09 7:09 
GeneralRe: parent child communication Pin
PIEBALDconsult21-Jan-09 7:13
mvePIEBALDconsult21-Jan-09 7:13 
AnswerRe: parent child communication Pin
DaveyM6921-Jan-09 7:21
professionalDaveyM6921-Jan-09 7:21 
The child form can access it's parent by its MDIParent property. The downside of this is it is of type Form rather than the actual class that you have created so you can't access any custom properties or methods.

The best way, is for the Child to raise events. The parent can subscribe to them immediately after it has instanciated the child. Very simple example below.
// FormParent.cs
using System;
using System.Windows.Forms;

public partial class FormParent : Form
{
    public FormParent()
    {
        InitializeComponent();
        IsMdiContainer = true;
        FormChild frmChild = new FormChild();
        frmChild.MdiParent = this;
        frmChild.ChildEvent += new EventHandler<ChildEventArgs>(frmChild_ChildEvent);
        frmChild.Show();
    }

    void frmChild_ChildEvent(object sender, ChildEventArgs e)
    {
        MessageBox.Show(e.Message);
    }
}
// FormChild.cs
using System;
using System.Windows.Forms;

public partial class FormChild : Form
{
    public event EventHandler<ChildEventArgs> ChildEvent;
    public FormChild()
    {
        InitializeComponent();
        Shown += new EventHandler(FormChild_Shown);
    }

    void FormChild_Shown(object sender, EventArgs e)
    {
        PerformChildEvent();
    }
    // Could be a property setter, a button click or anything
    // using a method for example only
    void PerformChildEvent()
    {
        OnChildEvent(new ChildEventArgs("Child Event"));
    }
    protected virtual void OnChildEvent(ChildEventArgs e)
    {
        EventHandler<ChildEventArgs> eh = ChildEvent;
        if (eh != null)
            eh(this, e);
    }
}
// ChildEventArgs.cs
public class ChildEventArgs : EventArgs
{
    private string m_Message;
    public ChildEventArgs(string message)
    {
        m_Message = message;
    }
    public string Message
    {
        get { return m_Message; }
    }
}


Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

GeneralRe: parent child communication Pin
bfis10813722-Jan-09 3:32
bfis10813722-Jan-09 3:32 
GeneralRe: parent child communication Pin
DaveyM6922-Jan-09 4:53
professionalDaveyM6922-Jan-09 4:53 
GeneralRe: parent child communication Pin
bfis10813722-Jan-09 4:56
bfis10813722-Jan-09 4:56 
GeneralRe: parent child communication Pin
DaveyM6922-Jan-09 5:09
professionalDaveyM6922-Jan-09 5:09 
GeneralRe: parent child communication Pin
bfis10813722-Jan-09 12:55
bfis10813722-Jan-09 12:55 
GeneralRe: parent child communication Pin
DaveyM6923-Jan-09 1:07
professionalDaveyM6923-Jan-09 1:07 
AnswerRe: parent child communication Pin
Giorgi Dalakishvili21-Jan-09 8:48
mentorGiorgi Dalakishvili21-Jan-09 8:48 
QuestiontoolStrip Quickie Pin
musefan21-Jan-09 6:09
musefan21-Jan-09 6:09 
AnswerRe: toolStrip Quickie Pin
#realJSOP21-Jan-09 8:00
professional#realJSOP21-Jan-09 8:00 
AnswerRe: toolStrip Quickie Pin
musefan21-Jan-09 8:29
musefan21-Jan-09 8:29 
Questionhow to get file present on another web site Pin
sushilbondre21-Jan-09 6:05
sushilbondre21-Jan-09 6:05 
AnswerRe: how to get file present on another web site Pin
musefan21-Jan-09 6:13
musefan21-Jan-09 6:13 
AnswerRe: how to get file present on another web site Pin
Dave Kreskowiak21-Jan-09 7:02
mveDave Kreskowiak21-Jan-09 7:02 
GeneralRe: how to get file present on another web site Pin
sushilbondre23-Jan-09 4:41
sushilbondre23-Jan-09 4:41 
GeneralRe: how to get file present on another web site Pin
Dave Kreskowiak23-Jan-09 13:49
mveDave Kreskowiak23-Jan-09 13:49 
GeneralRe: how to get file present on another web site Pin
sushilbondre24-Jan-09 4:17
sushilbondre24-Jan-09 4:17 
GeneralRe: how to get file present on another web site Pin
Dave Kreskowiak24-Jan-09 4:42
mveDave Kreskowiak24-Jan-09 4:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.