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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO.Ports;
using System.Windows.Forms;
using System.Threading;
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_2Darray : EventArgs
{
public int[,] target;
public EventArgs_2Darray(int[,] array)
{
target = array;
}
}
// Delegate declaration
public delegate void getCurveButtonHandler(object sender,
EventArgs_2Darray e);
public event getCurveButtonHandler click;
public getCurveButtonHandler clickSubscribers
{
get { return click; }
}
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object 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 settingsToolStripMenuItem_Click1(object sender,
EventArgs e)
{
// Make sure the port isn't already open
if (serialPort1.IsOpen)
{
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.ShowDialog() == DialogResult.OK)
{
if (settings.selectedPort != "")
{
// Set the serial port to the new settings
serialPort1.PortName = settings.selectedPort;
serialPort1.BaudRate = settings.selectedBaudrate;
showSettings();
}
else
{
MessageBox.Show("Error: Settings form returned with
no COM port selected.");
return; // bail out
}
}
else
{
MessageBox.Show("Error: buttonSetup_Click - Settings
dialog box did not return Okay.");
return; // bail out
}
// Open the port
try
{
serialPort1.Close();
serialPort1.Open();
menuStrip1.Items[1].Text = "Close Port";
showSettings();
}
catch (System.Exception ex)
{
MessageBox.Show("Error - setupToolStripMenuItem_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 openPortToolStripMenuItem_Click(object sender,
EventArgs e)
{
try
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
menuStrip1.Items[1].Text = "Open Port";
}
else
{
serialPort1.Open();
menuStrip1.Items[1].Text = "Close Port";
}
showSettings();
}
catch (System.Exception ex)
{
MessageBox.Show("Error - openPortToolStripMenuItem_Click
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 getCurvesToolStripMenuItem_Click(object sender,
EventArgs_2Darray e)
{
comm.DisplayWindow = rtbDisplay;
try
{
if (serialPort1.IsOpen)
{
comm.initializeIgnComm(serialPort1);
plotCurves(zedGraphControl1);
}
else
{
MessageBox.Show("Error: COM port closed.");
return; // bail out
}
}
catch (System.Exception ex)
{
MessageBox.Show("Error - findIgnButton_Click 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.PortName + " " +
serialPort1.BaudRate.ToString();
if (serialPort1.IsOpen)
{
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 zedGraphControl1_Load(object sender, EventArgs e)
{
}
private void plotCurves(ZedGraphControl zgc)
{
GraphPane plotCurve = zgc.GraphPane; //Get a
reference to the GraphPane
int[,] curvePointsArray;
curvePointsArray = 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(curvePointsArray); //Sort data into
list for plotting
LineItem ignCurve = plotCurve.AddCurve("WOT Curve 4", curve1,
Color.Red, SymbolType.Diamond);
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,arrayIndex] =
curveArray[0,genArrayIndex];
rpmValues[curveNumber,arrayIndex] =
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(rpmValues[0,i], advDegBTDC[0,i]);
}
return list1;
}
}
}
And here is the section code from the form designer file that applies to
this instance:
//
// getCurvesToolStripMenuItem
//
this.getCurvesToolStripMenuItem.Name =
"getCurvesToolStripMenuItem";
this.getCurvesToolStripMenuItem.Size = new
System.Drawing.Size(73, 20);
this.getCurvesToolStripMenuItem.Text = "Get Curves";
this.getCurvesToolStripMenuItem.Click += new
System.EventHandler(this.getCurvesToolStripMenuIte m_Click);
"Peter Duniho" wrote:
On Thu, 30 Oct 2008 14:02:01 -0700, dondigitech
<do*********@discussions.microsoft.comwrote:
// Create EventArgs subclass to pass in 2D array parameter
public class EventArgs_2Darray : EventArgs
{
[...]
}
// Delegate declaration
public delegate void getCurveButtonHandler(object sender, int[,]
array);
public event getCurveButtonHandler click;
My event handler is:
private void getCurvesToolStripMenuItem_Click(object sender,
EventArgs_2Darray )
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_2Darray, 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_2Darray".
Note that an alternative to declaring the delegate type would be to just
use the generic EventHandler<Ttype:
public event EventHandler<EventArgs_2Darrayclick;
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