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

C#

 
GeneralRe: Is there a good example of using multiple forms..... Pin
AussieLew2-Sep-10 18:53
AussieLew2-Sep-10 18:53 
GeneralRe: Is there a good example of using multiple forms..... Pin
DaveyM692-Sep-10 21:18
professionalDaveyM692-Sep-10 21:18 
GeneralRe: Is there a good example of using multiple forms..... Pin
glennPattonWork32-Sep-10 23:40
professionalglennPattonWork32-Sep-10 23:40 
GeneralRe: Is there a good example of using multiple forms..... Pin
AussieLew3-Sep-10 0:00
AussieLew3-Sep-10 0:00 
GeneralRe: Is there a good example of using multiple forms..... Pin
glennPattonWork33-Sep-10 0:03
professionalglennPattonWork33-Sep-10 0:03 
GeneralRe: Is there a good example of using multiple forms..... Pin
DaveyM693-Sep-10 1:07
professionalDaveyM693-Sep-10 1:07 
GeneralRe: Is there a good example of using multiple forms..... Pin
glennPattonWork33-Sep-10 1:12
professionalglennPattonWork33-Sep-10 1:12 
GeneralRe: Is there a good example of using multiple forms..... Pin
DaveyM693-Sep-10 3:01
professionalDaveyM693-Sep-10 3:01 
glennPattonWork wrote:
all seems to be forward and not backward passing


No. Have a look at this exmple that does exactly what you want. Anything you don't understand, just ask!
C#
// ThreeIntsEventArgs.cs
/* Arguments and delegate for FormChild.ThreeIntsEvent */

using System;

public delegate void ThreeIntsEventHandler(object sender, ThreeIntsEventArgs e);

public class ThreeIntsEventArgs : EventArgs
{
    private int x;
    private int y;
    private int z;

    public ThreeIntsEventArgs(int x, int y, int z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public int X
    {
        get { return x; }
    }
    public int Y
    {
        get { return y; }
    }
    public int Z
    {
        get { return z; }
    }
}

C#
// FormChild.cs
/* Click OK to raise event sending the three values */

using System.Drawing;
using System.Windows.Forms;

public partial class FormChild : Form
{
    public event ThreeIntsEventHandler ThreeInts;

    private NumericUpDown numericUpDownX;
    private NumericUpDown numericUpDownY;
    private NumericUpDown numericUpDownZ;
    private Button buttonOK;

    public FormChild()
    {
        InitializeComponent();
        // numericUpDownX
        numericUpDownX = new NumericUpDown();
        numericUpDownX.Location = new Point(12, 12);
        // numericUpDownY
        numericUpDownY = new NumericUpDown();
        numericUpDownY.Location = new Point(12, 36);
        // numericUpDownZ
        numericUpDownZ = new NumericUpDown();
        numericUpDownZ.Location = new Point(12, 60);
        // buttonOK
        buttonOK = new Button();
        buttonOK.Text = "OK";
        buttonOK.Location = new Point(12, 84);
        buttonOK.Click += new System.EventHandler(buttonOK_Click);
        // Add controls
        Controls.AddRange(new Control[] { numericUpDownX, numericUpDownY, numericUpDownZ, buttonOK });
        AcceptButton = buttonOK;
    }

    private void buttonOK_Click(object sender, System.EventArgs e)
    {
        OnThreeInts(new ThreeIntsEventArgs((int)numericUpDownX.Value, (int)numericUpDownY.Value, (int)numericUpDownZ.Value));
    }
    protected virtual void OnThreeInts(ThreeIntsEventArgs e)
    {
        ThreeIntsEventHandler eh = ThreeInts;
        if (eh != null)
            eh(this, e);
    }
}

C#
// FormParent.cs
/* Click the Form tho open a new child and subscribe to the event */

using System.Windows.Forms;

public partial class FormParent : Form
{
    public FormParent()
    {
        InitializeComponent();
        Click += new System.EventHandler(FormParent_Click);
    }

    private void FormParent_Click(object sender, System.EventArgs e)
    {
        FormChild formChild = new FormChild();
        // Subscribe
        formChild.ThreeInts += new ThreeIntsEventHandler(formChild_ThreeInts);
        formChild.Show();
    }
    // Handle event
    private void formChild_ThreeInts(object sender, ThreeIntsEventArgs e)
    {
        MessageBox.Show(string.Format("X: {0}, Y: {1}, Z:{2}", e.X, e.Y, e.Z));
    }
}

Dave

If this helped, please vote & accept answer!


Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

GeneralRe: Is there a good example of using multiple forms..... Pin
glennPattonWork33-Sep-10 5:04
professionalglennPattonWork33-Sep-10 5:04 
GeneralRe: Is there a good example of using multiple forms..... Pin
DaveyM693-Sep-10 7:26
professionalDaveyM693-Sep-10 7:26 
GeneralRe: Is there a good example of using multiple forms..... Pin
DaveyM693-Sep-10 9:05
professionalDaveyM693-Sep-10 9:05 
QuestionHow to call base class implementation method ? Pin
Yanshof2-Sep-10 2:32
Yanshof2-Sep-10 2:32 
AnswerRe: How to call base class implementation method ? Pin
phil.o2-Sep-10 2:37
professionalphil.o2-Sep-10 2:37 
GeneralRe: How to call base class implementation method ? Pin
Yanshof2-Sep-10 2:44
Yanshof2-Sep-10 2:44 
GeneralRe: How to call base class implementation method ? [modified] Pin
phil.o2-Sep-10 4:05
professionalphil.o2-Sep-10 4:05 
AnswerRe: How to call base class implementation method ? Pin
Ian Shlasko2-Sep-10 2:51
Ian Shlasko2-Sep-10 2:51 
GeneralRe: How to call base class implementation method ? Pin
Yanshof2-Sep-10 2:54
Yanshof2-Sep-10 2:54 
GeneralRe: How to call base class implementation method ? Pin
johannesnestler2-Sep-10 3:05
johannesnestler2-Sep-10 3:05 
GeneralRe: How to call base class implementation method ? Pin
Pete O'Hanlon2-Sep-10 3:35
mvePete O'Hanlon2-Sep-10 3:35 
AnswerRe: How to call base class implementation method ? Pin
Ennis Ray Lynch, Jr.2-Sep-10 2:54
Ennis Ray Lynch, Jr.2-Sep-10 2:54 
AnswerRe: How to call base class implementation method ? Pin
johannesnestler2-Sep-10 3:03
johannesnestler2-Sep-10 3:03 
AnswerRe: How to call base class implementation method ? Pin
PIEBALDconsult2-Sep-10 3:22
mvePIEBALDconsult2-Sep-10 3:22 
AnswerRe: How to call base class implementation method ? Pin
Paul Michalik2-Sep-10 21:44
Paul Michalik2-Sep-10 21:44 
QuestionClose iframe in Div Pin
SatyaKeerthi151-Sep-10 23:41
SatyaKeerthi151-Sep-10 23:41 
AnswerRe: Close iframe in Div Pin
DaveyM692-Sep-10 0:01
professionalDaveyM692-Sep-10 0:01 

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.