我一直在搜索stackoverflow和其他网站两天试图找出为什么我得到一个空委托。我讨厌发布一个已经回答的问题,但我对这个问题感到很遗憾。如果这已经在本网站的某个地方,请原谅我。
我的目标是将一些值从一种形式传递到另一种形式。但delPassData始终为空。
我的主要表单是Form1,我正在尝试从另一个表单formNewThread向它发送值。这是formNewThread中的相关代码:
public partial class formNewThread : Form
{
public delegate void delPassDataEventHandler(string where, string newThd); //declare delegate to communicate with Form1
public event delPassDataEventHandler delPassData;
private string newThread;
private string whereUsed;
public formNewThread(string whereTo, ComboBox cb)
{
InitializeComponent();
whereUsed = whereTo;
... removed some code here
}
public formNewThread()
{
InitializeComponent();
}
private void buttonPublish_Click(object sender, EventArgs e)
{
newThread = textNewThread.Text;
... removed code here
if (delPassData != null)
delPassData(whereUsed, newThread);
this.Close();
}
Form1中的相关代码是:
public Form1()
{
try
{
InitializeComponent();
frmNewThd.delPassData += new formNewThread.delPassDataEventHandler(frmNewThd_delPassData);
newCsg.delPassData += new NewCsg.delPassDataEventHandler(newCsg_delPassData);
... removed code
}
void frmNewThd_delPassData(string where, string newThd) //this routine communicates with with delegate in form formNewThread
{
if (where == "Lnr1")
{
comboLinerThread.Text = newThd;
}
else if (where == "Body")
{
comboBodyThdDown.Text = newThd;
}
else if (where == "SetTool")
{
comboSetToolThd.Text = newThd;
}
}
这是我测试并用于实现您在上面第一个程序中看到的代理的简单模型。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SendDataWithDelegate
{
public partial class Form1 : Form
{
private Form2 frm2 = new Form2();
private Form3 frm3 = new Form3();
public Form1()
{
InitializeComponent();
frm2.delPassData += new Form2.delPassDataEventHandler(frm2_delPassData); //declaring a new event handler for Form2
frm3.delPassData += new Form3.delPassDataEventHandler(frm3_delPassData); //declaring a new event handler for Form3
}
void frm3_delPassData(double od, double id, double length, double yield) //this routine communicates with delegate in Form3
{
labelOD.Text = String.Format("{0:F3}", od);
labelID.Text = String.Format("{0:F3}", id);
labelLength.Text = String.Format("{0:F3}", length);
labelYield.Text = String.Format("{0:N0}", yield);
}
void frm2_delPassData(string city, string state) //this routine communicates with delegate in Form2
{
labelCity.Text = city;
labelState.Text = state;
}
private void buttonOpenForm2_Click(object sender, EventArgs e) //launch Form2
{
frm2.Show();
}
private void buttonOpenForm3_Click(object sender, EventArgs e) //launch Form3
{
frm3.Show();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SendDataWithDelegate
{
public partial class Form2 : Form
{
public delegate void delPassDataEventHandler(string city, string state); //declare a delegate to communicate with Form1
public event delPassDataEventHandler delPassData; //declare an event name for the delegate's event handler
public Form2()
{
InitializeComponent();
}
private void buttonSendData_Click(object sender, EventArgs e)
{
if (delPassData != null)
delPassData(textCity.Text, textState.Text); //executes if the "delPassData event" occurrs
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SendDataWithDelegate
{
public partial class Form3 : Form
{
public delegate void delPassDataEventHandler(double od, double id, double length, double yield); //declare a delegate to communicate with Form1
public event delPassDataEventHandler delPassData; //declare an event name for the delegate's event handler
double ODia;
double IDia;
double Len;
double Sy;
public Form3()
{
InitializeComponent();
}
private void buttonSendData_Click(object sender, EventArgs e)
{
ODia = Convert.ToDouble(textOD.Text);
IDia = Convert.ToDouble(textID.Text);
Len = Convert.ToDouble(textLength.Text);
Sy = Convert.ToDouble(textYield.Text);
if (delPassData != null)
delPassData(ODia, IDia, Len, Sy); //executes if the "delPassData event" occurrs
}
}
}