473,854 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to pass more than two parameters in the event handler

Can anyone suggest me to pass more parameters other than two parameter for
events like the following?

Event:
Onbutton_click( object sender, EventArgs e)"

Event handler:
button.Click += new EventHandler(On button_click);

I want to pass more information related that event. & want to use that
information in the event.
Jul 17 '08
24 55279
I still can't seem to get this to work. I created the event but was unable to
get anything to happen when I clicked on the button. This was the code in the
designer form file:

this.click += new
getCurveButtonH andler(this.get CurvesToolStrip MenuItem_Click) ;

So I've done the following in my main form file:

// Create EventArgs subclass to pass in 2D array parameter
public class EventArgs_2Darr ay : EventArgs
{
public int[,] curveArray;
public EventArgs_2Darr ay(int[,] array)
{
curveArray = array;
}
}
// Delegate declaration
public delegate void getCurveButtonH andler(object sender, int[,]
array);
public event getCurveButtonH andler click;

My event handler is:

private void getCurvesToolSt ripMenuItem_Cli ck(object sender,
EventArgs_2Darr ay )
{
comm.DisplayWin dow = rtbDisplay;
try
{
if (serialPort1.Is Open)
{
comm.initialize IgnComm(serialP ort1);
plotCurves(zedG raphControl1, curveArray);
}
else
{
MessageBox.Show ("Error: COM port closed.");
return; // bail out
}
}
catch (System.Excepti on ex)
{
MessageBox.Show ("Error - findIgnButton_C lick Exception: " +
ex);
}

}

I still am not seeing how I can pass int[,] curveArray in as a parameter
because obviously I'm getting "The name 'curveArray' does not exist in the
current context" error. And in my designer form the click event is created as
such:

this.getCurvesT oolStripMenuIte m.Click += new
getCurveButtonH andler(this.get CurvesToolStrip MenuItem_Click) ;

The event Click built into the ToolStripMenuIt em is handled by the
EventHandler which only passes in the 2 parameters (object sender, EventArgs
e). So, of course I get the "Error1 No overload for
'getCurvesToolS tripMenuItem_Cl ick' matches delegate
'PCComm.frmMain .getCurveButton Handler' error. Since the ToolStripMenuIt em
class is being used to initialize all the items on the menu strip i've used
(I have 3 which include the getCurvesToolSt ripMenuItem) is there a way to
just make this single item work passing in the 2D array parameter. I've tried
copying the entire ToolStripMenuIt em class and adding another event that uses
my event handler getCurveButtonH andler but seems like this is not the best
solution. Need help!

"Jeff Johnson" wrote:
"dondigitec h LaPel" <dl****@dynaonl ine.comwrote in message
news:uE******** ******@TK2MSFTN GP04.phx.gbl...
I am also having a similar problem.I would like to pass in an extra
parameter in my event handler

I don't see the post that you replied to, so I'm making assumptions about
the original question.

My first assumption is that you're defining your OWN event handler and not
dealing with an event model that is out of your control. The .NET standard
is to pass only two parameters in an event handler: an instance of Object
that represents the object which raised the event (the sender) and an
instance of EventArgs--or a class derived from EventArgs--which contains all
the necessary information that a subscriber (consumer) might need. The part
about derived classes is important. If you need to provide information to
your event consumers then you should derive a class from EventArgs and add
properties to that class to hold that necessary data. (Well, unless the
Framework already provides a derived class which gives you exactly what you
need.)

Most of the time these properties are for one-way information: from the
sender to the consumer, but sometimes the consumer will pass information
back to the sender (an event which can be canceled is a perfect example of
this).
Oct 30 '08 #21
On Thu, 30 Oct 2008 14:02:01 -0700, dondigitech
<do*********@di scussions.micro soft.comwrote:
// Create EventArgs subclass to pass in 2D array parameter
public class EventArgs_2Darr ay : EventArgs
{
[...]
}
// Delegate declaration
public delegate void getCurveButtonH andler(object sender, int[,]
array);
public event getCurveButtonH andler click;

My event handler is:

private void getCurvesToolSt ripMenuItem_Cli ck(object sender,
EventArgs_2Darr ay )
It would really help if you'd post the _actual_ code, copied and pasted.
Since your method declaration is missing the actual argument name for the
second argument, the above obviously isn't from a real program, which
makes it hard to provide a real answer.

That said...

Your delegate type needs to match the event handler itself. Your event
handler method takes as the second argument an instance of
EventArgs_2Darr ay, but your delegate type takes as the second argument a
two-dimensional "int[,]" array.

The most obvious fix is to change the argument type in the delegate type
from "int[,]" to "EventArgs_2Dar ray".

Note that an alternative to declaring the delegate type would be to just
use the generic EventHandler<Tt ype:

public event EventHandler<Ev entArgs_2Darray click;

Now, there are other issues with the code that you've posted, not the
least of which is that the "Click" event is already a well-defined .NET
Control-class event that uses the EventHandler delegate type, and
redefining that for your own purpose is probably a big mistake. Other
issues are less significant, such as the use of a public field instead of
a property in the EventArgs sub-class, while other issues may be even more
significant, such as the question of what it is exactly you really want
this event handler to do and how you expect it to do it (so far, you've
been relatively vague on those specifics, making it difficult to provide a
good answer).

But at the very least, hopefully the above should help you move closer to
code that compiles. Once we get there, it'll probably be easier to answer
the really interesting parts of the question. :)

Pete
Oct 30 '08 #22
I did have both the delegate and event handler passing in the same params but
I changed it to see what would happen. Basically I have a class called
CurveData which reads data and calculates values and returns a 2D array which
I am trying to pass into the sortData and plotCurves methods in the main
form. A list is supposed to be generated which is then used in the plotCurves
method to display a graph. This is frustrating becuase it seems like i'm
close to getting this done but am having problems attacking this situation of
getting information from curveArray into this main form file. I hope this
clarifies my objective and the problems that I'm having. If you have any
advice for a different approach, it'd be much appreciated. Anywho, here is
the code from my main form file:

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO.Ports ;
using System.Windows. Forms;
using System.Threadin g;
using PCComm;
using PortSet;
using ZedGraph;

namespace PCComm
{
public partial class frmMain : Form
{
CurveData comm = new CurveData();
string transType = string.Empty;

// Create EventArgs subclass to pass in 2D array parameter
public class EventArgs_2Darr ay : EventArgs
{
public int[,] target;
public EventArgs_2Darr ay(int[,] array)
{
target = array;
}
}
// Delegate declaration
public delegate void getCurveButtonH andler(object sender,
EventArgs_2Darr ay e);
public event getCurveButtonH andler click;
public getCurveButtonH andler clickSubscriber s
{
get { return click; }
}

public frmMain()
{
InitializeCompo nent();
}

private void frmMain_Load(ob ject sender, EventArgs e)
{
//LoadValues();
//SetDefaults();
}

private void LoadValues()
{
}
//=============== =============== =============== =============== =============== ============
# region MENU STRIP CONTROL
/// <summary>
/// Menu strip which controls serial port settings, opening and
closing the serial
/// port, acquiring ignition timing data as well as plotting out the
advance curves.
/// These functions below are executed in the event the user clicks
on a button on the
/// menu strip at the top of the form.
/// </summary>
//
//--------------------------------------------------------------------
//<<<<<<<<< SERIAL PORT SETTINGS MENU >>>>>>>>>>//
//--------------------------------------------------------------------
// Com port & baud rate settings can be changed by opening this menu.
//--------------------------------------------------------------------
private void settingsToolStr ipMenuItem_Clic k1(object sender,
EventArgs e)
{
// Make sure the port isn't already open
if (serialPort1.Is Open)
{
MessageBox.Show ("The port must be closed before changing the
settings.");
return;
}
else
{
// Create an instance of the settings form
PortSettings settings = new PortSettings();
if (settings.ShowD ialog() == DialogResult.OK )
{
if (settings.selec tedPort != "")
{
// Set the serial port to the new settings
serialPort1.Por tName = settings.select edPort;
serialPort1.Bau dRate = settings.select edBaudrate;
showSettings();
}
else
{
MessageBox.Show ("Error: Settings form returned with
no COM port selected.");
return; // bail out
}
}
else
{
MessageBox.Show ("Error: buttonSetup_Cli ck - Settings
dialog box did not return Okay.");
return; // bail out
}
// Open the port
try
{
serialPort1.Clo se();
serialPort1.Ope n();
menuStrip1.Item s[1].Text = "Close Port";
showSettings();
}
catch (System.Excepti on ex)
{
MessageBox.Show ("Error - setupToolStripM enuItem_Click
Exception: " + ex);
}
}
}

//--------------------------------------------------------------------
//<<<<<<<<< OPEN/CLOSE PORT BUTTON >>>>>>>>>>//
//--------------------------------------------------------------------
// You can open or close the communication port by clicking this
button.
// Default settings are acquired if you have not changed them before
// clicking this button.
//--------------------------------------------------------------------
private void openPortToolStr ipMenuItem_Clic k(object sender,
EventArgs e)
{
try
{
if (serialPort1.Is Open)
{
serialPort1.Clo se();
menuStrip1.Item s[1].Text = "Open Port";
}
else
{
serialPort1.Ope n();
menuStrip1.Item s[1].Text = "Close Port";
}

showSettings();
}
catch (System.Excepti on ex)
{
MessageBox.Show ("Error - openPortToolStr ipMenuItem_Clic k
Exception: " + ex);
}
}

private void GroupBox1_Enter (object sender, EventArgs e)
{

}

//--------------------------------------------------------------------
//<<<<<<<<< GET CURVES BUTTON >>>>>>>>>>//
//--------------------------------------------------------------------
// Once communication is established with the ignition unit. You
// are able to download ignition data by clicking on "Get Curves"
button.
// The ignition data downloaded from EEPROM is sorted and
// the advances are calculated and then plotted on the graph provided.
//--------------------------------------------------------------------
private void getCurvesToolSt ripMenuItem_Cli ck(object sender,
EventArgs_2Darr ay e)
{
comm.DisplayWin dow = rtbDisplay;
try
{
if (serialPort1.Is Open)
{
comm.initialize IgnComm(serialP ort1);
plotCurves(zedG raphControl1);
}
else
{
MessageBox.Show ("Error: COM port closed.");
return; // bail out
}
}
catch (System.Excepti on ex)
{
MessageBox.Show ("Error - findIgnButton_C lick Exception: " +
ex);
}

}
#endregion
//=============== =============== =============== ===
# region CURVE ID HEADER
/// <summary>
/// The header on the Curve Identifier application displays serial
port status,
/// serial port name and baud rate.
/// </summary>
//=============== =============== =============== ===
private void showSettings()
{
this.Text = "DYNATEK IGNITIONS - " +
serialPort1.Por tName + " " +
serialPort1.Bau dRate.ToString( );
if (serialPort1.Is Open)
{
this.Text += " - Port is open";
}
else
{
this.Text += " - Port is closed";
}
}
#endregion
//=============== =============== =============== ===
# region GRAPH CURVES
/// <summary>
/// This function controls the graphing of the ignition advance
curves.
/// </summary>
//=============== =============== =============== ===
private void zedGraphControl 1_Load(object sender, EventArgs e)
{
}

private void plotCurves(ZedG raphControl zgc)
{
GraphPane plotCurve = zgc.GraphPane; //Get a
reference to the GraphPane

int[,] curvePointsArra y;
curvePointsArra y = comm.GetData();

// Set the Titles
plotCurve.Title .Text = "DFS726 IGNITION TIMING\n";
plotCurve.XAxis .Title.Text = "RPM(x100)" ;
plotCurve.YAxis .Title.Text = "Advance Angle(Degrees BTDC)";

PointPairList curve1 = new PointPairList() ; //Create curve1
list
sortData(curveP ointsArray); //Sort data into
list for plotting

LineItem ignCurve = plotCurve.AddCu rve("WOT Curve 4", curve1,
Color.Red, SymbolType.Diam ond);
zgc.AxisChange( );

}
#endregion

//--------------------------------------------------
// SORT ARRAY DATA TO POINTS FUNCTION
//--------------------------------------------------
private PointPairList sortData(int[,] curveArray)
{
int numberOfCurves = 8;
int[,] advDegBTDC = new int[numberOfCurves, 8];
int[,] rpmValues = new int[numberOfCurves, 8];
int curveNumber = 0;
int arrayIndex = 0;
int genArrayIndex = 0;

// Create array of arrays to store rpm and advance degree values
for ignition curves
//for (int q = 0; q < numberOfCurves; q++)
//{
// advDegBTDC[q,] = new int[8];
// rpmValues[q,] = new int[8];
//}

// Copy data from rpmArray and advArray into rpmValues and
advDegBTDC arrays used for plots
for (curveNumber = 0; curveNumber < numberOfCurves; curveNumber++)
{
for (arrayIndex = 0; arrayIndex < 8; arrayIndex++)
{
advDegBTDC[curveNumber,arr ayIndex] =
curveArray[0,genArrayIndex];
rpmValues[curveNumber,arr ayIndex] =
curveArray[1,genArrayIndex];
genArrayIndex++ ;
}
}
PointPairList list1 = new PointPairList() ;

// TEST: Just sending the first curve (8 data points) to list1
to graph
for (int i = 0; i < 8; i++)
{
list1.Add(rpmVa lues[0,i], advDegBTDC[0,i]);
}
return list1;

}
}
}

And here is the section code from the form designer file that applies to
this instance:

//
// getCurvesToolSt ripMenuItem
//
this.getCurvesT oolStripMenuIte m.Name =
"getCurvesToolS tripMenuItem";
this.getCurvesT oolStripMenuIte m.Size = new
System.Drawing. Size(73, 20);
this.getCurvesT oolStripMenuIte m.Text = "Get Curves";
this.getCurvesT oolStripMenuIte m.Click += new
System.EventHan dler(this.getCu rvesToolStripMe nuItem_Click);

"Peter Duniho" wrote:
On Thu, 30 Oct 2008 14:02:01 -0700, dondigitech
<do*********@di scussions.micro soft.comwrote:
// Create EventArgs subclass to pass in 2D array parameter
public class EventArgs_2Darr ay : EventArgs
{
[...]
}
// Delegate declaration
public delegate void getCurveButtonH andler(object sender, int[,]
array);
public event getCurveButtonH andler click;

My event handler is:

private void getCurvesToolSt ripMenuItem_Cli ck(object sender,
EventArgs_2Darr ay )

It would really help if you'd post the _actual_ code, copied and pasted.
Since your method declaration is missing the actual argument name for the
second argument, the above obviously isn't from a real program, which
makes it hard to provide a real answer.

That said...

Your delegate type needs to match the event handler itself. Your event
handler method takes as the second argument an instance of
EventArgs_2Darr ay, but your delegate type takes as the second argument a
two-dimensional "int[,]" array.

The most obvious fix is to change the argument type in the delegate type
from "int[,]" to "EventArgs_2Dar ray".

Note that an alternative to declaring the delegate type would be to just
use the generic EventHandler<Tt ype:

public event EventHandler<Ev entArgs_2Darray click;

Now, there are other issues with the code that you've posted, not the
least of which is that the "Click" event is already a well-defined .NET
Control-class event that uses the EventHandler delegate type, and
redefining that for your own purpose is probably a big mistake. Other
issues are less significant, such as the use of a public field instead of
a property in the EventArgs sub-class, while other issues may be even more
significant, such as the question of what it is exactly you really want
this event handler to do and how you expect it to do it (so far, you've
been relatively vague on those specifics, making it difficult to provide a
good answer).

But at the very least, hopefully the above should help you move closer to
code that compiles. Once we get there, it'll probably be easier to answer
the really interesting parts of the question. :)

Pete
Oct 30 '08 #23
On Fri, 31 Oct 2008 09:42:01 -0700, dondigitech
<do*********@di scussions.micro soft.comwrote:
Formatting got all messed up again! I wasted my time copying into
notepad and
spacing it out!!
I really don't know what you mean. Here, the code looks reasonably fine.
Maybe the Microsoft Communities web site just isn't showing it correctly.

The problem isn't in the formatting, it's in the content.

See:
http://www.yoda.arachsys.com/csharp/complete.html
http://www.yoda.arachsys.com/csharp/incomplete.html
Oct 31 '08 #24
"dondigitec h" <do*********@di scussions.micro soft.comwrote in message
news:09******** *************** ***********@mic rosoft.com...
Ultimately the issue I am having is getting/passing the curveArray data to
the event handler.
Okay, this is where you lose me. I haven't looked too deeply into the code
because, quite frankly, there's way too much of it and I'm just not going to
take the time. I'm assuming that you've got a form, because you talked about
clicking a button. Now you want to pass something to the event handler.
Guess what: YOU CAN'T. The event handler is being called by the button, and
you don't have access to the button's internal code to control the way it
raises events and the data it passes. (I'm not even going to go into the
subject of inheriting from the button....)

So instead of going down the path of trying to get the desired data PASSED
IN to the event handler, you should instead be looking for a way to RETRIEVE
it once you're in the handler. It's your code; why wouldn't you have access
to the array you're looking for?
Oct 31 '08 #25

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
49591
by: Pavils Jurjans | last post by:
Hallo, I have been programming for restricted environments where Internet Explorer is a standard, so I haven't stumbled upon this problem until now, when I need to write a DOM-compatible code. The question is about best practices for passing parameters to an event function. I have, say, the following HTML:
2
2001
by: Adi | last post by:
Okay, this issue has been annoying me for a little while now. I need it to work on Mozilla 1.6 & IE 6 This is what I would like to be able to do: var row = document.createElement("TR"); var tdImage = document.createElement("TD"); var img = document.createElement("IMG"); img.src = "results.gif"; var theA = document.createElement("A");
7
4677
by: mg | last post by:
How can I call an event handler (e.g., Button1_Click) programmatically. That is, without having to click on the button with the mouse.
2
3747
by: Jose Suero | last post by:
Hi all I have a dynamically created button, I can add an event handler with: AddHandler button.click, AddressOf static_function This works great, but what I need is to create a function that takes the control, the event and the function as parameters, something like: function addevent(control as object, event as string, functionname as string)
15
26524
by: Amit D.Shinde | last post by:
I am adding a new picturebox control at runtime on the form How can i create click event handler for this control Amit Shinde
5
3872
by: james | last post by:
Hello, I am having a little trouble creating an event handler for a context menu toolstripmenuitem. I've seen various tutorials and so on, but I keep getting a bit stuck! So far I have a second class defining the eventargs I want to use: public class ApptEventArgs : EventArgs{ public int ApptUID; public String ApptOp;
2
2083
by: fusillo | last post by:
My code pass a value of a variable to the event handler's argument by means of eval statement here's: //statements setting evt for (var i=0; i<evt.periodi.length; i++){ //evt.periodi is an array of objs a=document.createElement("a"); a.onclick=eval('function (){ cancellaCal(' + evt.periodi.id_evt_cal
5
5206
by: Tenacious | last post by:
I am trying to shutdown a database server in the Application_End event handler on the Global.asax page. So far I am trying this only on the development server that comes with Visual Studio 2005. When I close the browser, this does not cause the Application_End event to trigger. It also will not occur when I shut down the development server. At this point the application is no longer running, so why didn't the event handler ever get called?...
7
1841
by: Andrus | last post by:
I noticed that DataGridView CellValidated() and other event handler first parameter is object: Grid.CellValidated+=new DataGridViewCellEventHandler(Grid_CellValidated); .... void Grid_CellValidated(object sender, DataGridViewCellEventArgs e) { .....
0
9903
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11041
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10692
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10766
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10373
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9525
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7083
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
4167
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.