469,579 Members | 1,899 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

How to access an event from another Form ?

Hi

I have created one form that has an event handler, coded in the
following lines :
(ON FORM1 : Picture Boxes, when clicked they fire events)

this.OnPADClick += new FrmPAD.OnPADClickEventHandler
(this.FrmPAD_OnPADClick);

private void ArrowUp_Click(object sender, System.EventArgs e)
{
int iNumToPass = 0;
System.EventArgs p = new System.EventArgs();
OnPADClick (this, p, iNumToPass);
return;
}

public delegate void OnPADClickEventHandler (object sender,
System.EventArgs e, long num);
public event OnPADClickEventHandler OnPADClick;

private void FrmPAD_OnPADClick (object sender, System.EventArgs e, long
theNum)
{
switch(theNum)
{
case 0:
MessageBox.Show("Click on PictureBox !");
break;
default:
break;
}
}

(ON FORM2, What I want to do) :

I need to get the event from another form (Form2), using the same kind
of function as "private void FrmPAD_OnPADClick(object sender,
System.EventArgs e, long theNum);"

Could anyone point me in the right direction... What piece of code to
add to Form2 to get events fired from Form1 ?

Thank you very much.

Nov 17 '05 #1
2 4413
Are they in the same namespace? The same class? If you can reference
Form2 from Form1 then you can set up a custom event in Form2 and have
Form1 subscribe to it. Wherever you create Form2 and have a reference
to it, you can do exactly what you do at the beginning of your code
block.

<reference to Form2>.<Form2 event you want to capture> +=
FrmPAD_OnPADClick (etc.)

Here's an example.

Form2 code

public event FrmPAD.OnPADClickEventHandler somethingHappened;

public void FunctionThatCausesEventToFire()
{
somethingHappened(this, new EventArgs());
}

Form1 code

Form2 instanceName = new Form2();
instanceName.somethingHappened += new
FrmPAD.OnPADClickEventHandler(this.FrmPAD_OnPADCli ck);

That should cause FrmPAD_OnPADClick to get called when the event
happens in Form2. Hope that helps.

Nov 17 '05 #2
Great! That works perfectly... I did try the same thing, but without
using an instantiated version of the form that fires the event...

What I did is to centralize all the form on an unique class (that
memorizes handles to the different forms) and to subscribe to the event
as you describe...

Thank you VERY VERY VERY much, that helps me a lot ! :)

Justin a écrit :
Are they in the same namespace? The same class? If you can reference
Form2 from Form1 then you can set up a custom event in Form2 and have
Form1 subscribe to it. Wherever you create Form2 and have a reference
to it, you can do exactly what you do at the beginning of your code
block.

<reference to Form2>.<Form2 event you want to capture> +=
FrmPAD_OnPADClick (etc.)

Here's an example.

Form2 code

public event FrmPAD.OnPADClickEventHandler somethingHappened;

public void FunctionThatCausesEventToFire()
{
somethingHappened(this, new EventArgs());
}

Form1 code

Form2 instanceName = new Form2();
instanceName.somethingHappened += new
FrmPAD.OnPADClickEventHandler(this.FrmPAD_OnPADCli ck);

That should cause FrmPAD_OnPADClick to get called when the event
happens in Form2. Hope that helps.


Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by JK Peck | last post: by
6 posts views Thread by Peter Frost | last post: by
9 posts views Thread by MacDermott | last post: by
49 posts views Thread by Yannick Turgeon | last post: by
55 posts views Thread by AnandaSim | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.