最近學習了委託的使用,爲了加深瞭解,下面使用委託方式,新建個winform,施一個button1,一個textbox1設置TextBox.Text實例,在vs2010下測試通過
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
{
delegate void MyDeleGate(string s); //1.定義委託結構
MyDeleGate mdg; //2.定義委託
//3.定義委託方法
public void func(string s)
{
textBox1.Text = s;
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//4.梆定委託方法
mdg= new MyDeleGate(func);
//5.調用委託
mdg("那是一陣風");
}
}
}