通过事件使用委托
事件在类中声明且生成,且通过使用同一个类或其他类中的委托与事件处理程序关联。包含事件的类用于发布事件。
public class So
{
public string SoNo { get; set; }
public string Customer { get; set; }
public string Inconterms { get; set; }
public string WbsNo { get; set; }
public string PoNo { get; set; }
public delegate void delLoadRow(So so);
public event delLoadRow eventRow;
List<So> sos = new List<So>();
public So()
{
}
public void LoadSo(string filename)
{
string[] lines = File.ReadAllLines("./so.txt");
foreach (string line in lines)
{
string[] item = line.Split('\t');
So s = new So()
{
SoNo = item[0],
Customer = item[1],
Inconterms = item[2],
WbsNo = item[4],
PoNo = item[5],
};
if (eventRow != null)
{
eventRow(s);
}
}
}
private void btnSearch_Click(object sender, EventArgs e)
{
So so = new So();
so.eventRow += So_eventRow;
so.LoadSo("./so.txt");
}
private void So_eventRow(So s)
{
ListViewItem item = new ListViewItem();
item.Text = s.SoNo;
item.SubItems.Add(s.Customer);
item.SubItems.Add(s.Inconterms);
item.SubItems.Add(s.WbsNo);
item.SubItems.Add(s.PoNo);
lsvMain.Items.Add(item);
}