C#委託調用實例

最近學習了委託的使用,爲了加深瞭解,下面使用委託方式,新建個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("那是一陣風");
        }  
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章