C#委托事件应用实例

委托

1.委托是什么?

个人理解是:

①委托是一种特殊的类

②委托可以绑定多个方法(含有相同的传入参数)

2.委托的应用

①初级应用

先定义委托事件:

public delegate void PerformCalculation(string text);
    
    
定义相关方法:


    
    
  1. public void Print1(string p1)
  2. {
  3.     MessageBox.Show( "Print1" + p1);
  4. }
  5. public void Print2(string p2)
  6. {
  7.     MessageBox.Show( "Print2" + p2);
  8. }
定义点击事件:


    
    
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. PerformCalculation Print = Print1;
  4. Print( "p1");
  5. }
  6. private void button2_Click(object sender, EventArgs e)
  7. {
  8. PerformCalculation Print = Print1;
  9. Print( "p1");
  10. }
即可实现委托调用不同的方法。

效果图:




②实战应用-进度条的实现

定义委托方法:

public delegate void DelegateMethod(int position, int maxValue);
    
    
定义相关类:


    
    
  1. public class TestDelegate
  2. {
  3. public DelegateMethod OnDelegate;
  4. public void DoDelegateMethod()
  5. {
  6. int maxValue = 100;
  7. for ( int i = 0; i < maxValue; i++)
  8. {
  9. if ( this.OnDelegate != null)
  10. {
  11. this.OnDelegate(i, maxValue);
  12. }
  13. }
  14. }
  15. }
定义点击事件:


    
    
  1. private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
  2. {
  3. TestDelegate test = new TestDelegate();
  4. this.textBox1.Text = "";
  5. this.progressBar1.Value = 0;
  6. test.OnDelegate = new DelegateMethod( delegate ( int i, int maxValue)
  7. {
  8. this.textBox1.Text += i.ToString() + Environment.NewLine;
  9. this.progressBar1.Maximum = maxValue;
  10. this.progressBar1.Value++;
  11. });
  12. test.DoDelegateMethod();
  13. }








委托

1.委托是什么?

个人理解是:

①委托是一种特殊的类

②委托可以绑定多个方法(含有相同的传入参数)

2.委托的应用

①初级应用

先定义委托事件:

public delegate void PerformCalculation(string text);
  
  
定义相关方法:


  
  
  1. public void Print1(string p1)
  2. {
  3.     MessageBox.Show( "Print1" + p1);
  4. }
  5. public void Print2(string p2)
  6. {
  7.     MessageBox.Show( "Print2" + p2);
  8. }
定义点击事件:


  
  
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. PerformCalculation Print = Print1;
  4. Print( "p1");
  5. }
  6. private void button2_Click(object sender, EventArgs e)
  7. {
  8. PerformCalculation Print = Print1;
  9. Print( "p1");
  10. }
即可实现委托调用不同的方法。

效果图:




②实战应用-进度条的实现

定义委托方法:

public delegate void DelegateMethod(int position, int maxValue);
  
  
定义相关类:


  
  
  1. public class TestDelegate
  2. {
  3. public DelegateMethod OnDelegate;
  4. public void DoDelegateMethod()
  5. {
  6. int maxValue = 100;
  7. for ( int i = 0; i < maxValue; i++)
  8. {
  9. if ( this.OnDelegate != null)
  10. {
  11. this.OnDelegate(i, maxValue);
  12. }
  13. }
  14. }
  15. }
定义点击事件:


  
  
  1. private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
  2. {
  3. TestDelegate test = new TestDelegate();
  4. this.textBox1.Text = "";
  5. this.progressBar1.Value = 0;
  6. test.OnDelegate = new DelegateMethod( delegate ( int i, int maxValue)
  7. {
  8. this.textBox1.Text += i.ToString() + Environment.NewLine;
  9. this.progressBar1.Maximum = maxValue;
  10. this.progressBar1.Value++;
  11. });
  12. test.DoDelegateMethod();
  13. }








猜你喜欢

转载自blog.csdn.net/zhaobinbin2015/article/details/81363839