首页上一页 1 下一页尾页 2 条记录 1/1页
C#开发委托
发表在C#图书答疑
2010-12-15
是否精华
是
否
版块置顶:
是
否
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 委托列表
{
public partial class Form1 : Form
{
public delegate void MyDelegate(string text);
MyDelegate mainDelegate;
private void method1(string text)
{
textBox1.AppendText("方法一被调用(" + text + ")\r\n");
}
private void method2(string text)
{
textBox1.AppendText("方法二被调用(" + text + ")\r\n");
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (mainDelegate == null)
{
mainDelegate = this.method1;
}
else
{
mainDelegate = mainDelegate + new MyDelegate(method1);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (mainDelegate == null)
{
mainDelegate = this.method2;
}
else
{
mainDelegate+=method2;
}
}
private void button3_Click(object sender, EventArgs e)
{
if (mainDelegate == null)
return;
mainDelegate -= method1;
}
private void button4_Click(object sender, EventArgs e)
{
if (mainDelegate == null)
return;
else
mainDelegate -= method2;
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox1.AppendText("调用列表\r\n");
foreach (MyDelegate del in mainDelegate.GetInvocationList())
{
textBox1.AppendText(del.Method.Name + "\r\n");
}
textBox1.AppendText("================================================\r\n");
mainDelegate("传递给委托的参数");
}
}
}
小科老师,不是点button3,是点button5。我刚开始点击button1,然后点击button5,结果列表有个方法,然后我再点击button1,再点击button5,列表有出现一个方法。接着我点击button3,然后点击button5,列表被删除了一个方法,列表还剩一个没删除,我这个时候再点击button3,然后点击button5,就报错了,错误就是:具体错误为:未将对象引用设置到对象的实例。请问这是什么问题呢?
希望我说的清楚了,请指点,小科老师!
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 委托列表
{
public partial class Form1 : Form
{
public delegate void MyDelegate(string text);
MyDelegate mainDelegate;
private void method1(string text)
{
textBox1.AppendText("方法一被调用(" + text + ")\r\n");
}
private void method2(string text)
{
textBox1.AppendText("方法二被调用(" + text + ")\r\n");
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (mainDelegate == null)
{
mainDelegate = this.method1;
}
else
{
mainDelegate = mainDelegate + new MyDelegate(method1);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (mainDelegate == null)
{
mainDelegate = this.method2;
}
else
{
mainDelegate+=method2;
}
}
private void button3_Click(object sender, EventArgs e)
{
if (mainDelegate == null)
return;
mainDelegate -= method1;
}
private void button4_Click(object sender, EventArgs e)
{
if (mainDelegate == null)
return;
else
mainDelegate -= method2;
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox1.AppendText("调用列表\r\n");
foreach (MyDelegate del in mainDelegate.GetInvocationList())
{
textBox1.AppendText(del.Method.Name + "\r\n");
}
textBox1.AppendText("================================================\r\n");
mainDelegate("传递给委托的参数");
}
}
}
小科老师,不是点button3,是点button5。我刚开始点击button1,然后点击button5,结果列表有个方法,然后我再点击button1,再点击button5,列表有出现一个方法。接着我点击button3,然后点击button5,列表被删除了一个方法,列表还剩一个没删除,我这个时候再点击button3,然后点击button5,就报错了,错误就是:具体错误为:未将对象引用设置到对象的实例。请问这是什么问题呢?
希望我说的清楚了,请指点,小科老师!