473,382 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 4578
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: JK Peck | last post by:
I have a fairly large Access application that ran correctly in Access 2000. After upgrading to Access 2003 (and recompiling and updating references), it reliably crashes at a certain point. If I...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
3
by: Christopher Koh | last post by:
how do you stop Access from saving any changed data in your tables and queries? like i just add or change data on the table/query tables,then click on X (exit)because i have no intention of saving...
9
by: MacDermott | last post by:
I have an Access MDB which instantiates a class in a custom DLL, manipulates it for a while, then sets it equal nothing. The MDB does other things,too, and generally behaves itself as desired....
13
by: Manuel Lopez | last post by:
I have a puzzling form timer problem that I didn't experience prior to Access 2003 (though I'm not sure access 2003 is to blame). Here's the situation: a computer has two access 2003 databases on...
49
by: Yannick Turgeon | last post by:
Hello, We are in the process of examining our current main application. We have to do some major changes and, in the process, are questionning/validating the use of MS Access as front-end. The...
55
by: AnandaSim | last post by:
I just had a google through this NG but have not seen mention of Erik Rucker's blog entry and the new Jet: http://blogs.msdn.com/access/archive/2005/10/05/477549.aspx mentioned by Mike...
8
by: Sid | last post by:
I hope someone could help me with this. I am trying to setup a criteria to decide when to allow/not allow user to click on the check box. logically it looks simple but I am not able to...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.