
|
Visual C# Windows Form Using Delegates Example
|
|---|
|
Visual C# Windows Form Using Delegates Example
Learn how to employ asynchronous delegates using a Windows Form example written with VS.NET.
Category: .Net Framework, Visual C#, Windows Forms, Delegates,
Posted 12/13/2002,
By Mike G.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace DelegateExample
{
///
/// MTG's C# Delegate Example.
///
public class Form1 : System.Windows.Forms.Form
{
private static int intNumber = 0;
///
/// Creates a new delegate.
///
public delegate void MTGDelegate(int Number, Operation Op);
///
/// Creates a new delegate to be used asynchronously.
///
public delegate void MTGAsyncDelegate();
///
/// This is the actual instance of the delegate which acts like
/// a real method the code will call. Code doesn't call the
/// delegate itself but rather an instance of the delegate.
/// dlgInvoke is not a real method but rather an instance of one.
///
private MTGDelegate dlgInvoke = new MTGDelegate(MTGDelegateTarget);
///
/// This enumerator will determine if we are subtracting or
/// adding to the number.
///
public enum Operation
{
Add, Substract
}
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnSubtract;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label lblNumber;
private System.Windows.Forms.Button btnAsync;
private System.Windows.Forms.Label lblAsync;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.lblNumber = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.btnSubtract = new System.Windows.Forms.Button();
this.btnAdd = new System.Windows.Forms.Button();
this.btnAsync = new System.Windows.Forms.Button();
this.lblAsync = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.lblAsync,
this.btnAsync,
this.lblNumber,
this.label1,
this.btnSubtract,
this.btnAdd});
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(248, 288);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "MTG\'s C# Delegate Example - dotnetfun.com";
//
// lblNumber
//
this.lblNumber.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblNumber.Location = new System.Drawing.Point(8, 128);
this.lblNumber.Name = "lblNumber";
this.lblNumber.Size = new System.Drawing.Size(232, 40);
this.lblNumber.TabIndex = 3;
this.lblNumber.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label1
//
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.Location = new System.Drawing.Point(8, 104);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(232, 23);
this.label1.TabIndex = 2;
this.label1.Text = "Current Number:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// btnSubtract
//
this.btnSubtract.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnSubtract.Location = new System.Drawing.Point(8, 64);
this.btnSubtract.Name = "btnSubtract";
this.btnSubtract.Size = new System.Drawing.Size(232, 32);
this.btnSubtract.TabIndex = 1;
this.btnSubtract.Text = "Subtract 1 from Total via Delegate";
this.btnSubtract.Click += new System.EventHandler(this.button1_Click);
//
// btnAdd
//
this.btnAdd.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnAdd.Location = new System.Drawing.Point(8, 24);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(232, 32);
this.btnAdd.TabIndex = 0;
this.btnAdd.Text = "Add 1 to Total via Delegate";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnAsync
//
this.btnAsync.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnAsync.Location = new System.Drawing.Point(8, 184);
this.btnAsync.Name = "btnAsync";
this.btnAsync.Size = new System.Drawing.Size(232, 32);
this.btnAsync.TabIndex = 4;
this.btnAsync.Text = "Invoke Asynchronous Method";
this.btnAsync.Click += new System.EventHandler(this.btnAsync_Click);
//
// lblAsync
//
this.lblAsync.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblAsync.Location = new System.Drawing.Point(8, 216);
this.lblAsync.Name = "lblAsync";
this.lblAsync.Size = new System.Drawing.Size(232, 64);
this.lblAsync.TabIndex = 5;
this.lblAsync.Text = "Asynchronous Status: not running";
this.lblAsync.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(264, 302);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.groupBox1});
this.Name = "Form1";
this.Text = "MTG\'s C# Delegate Example";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
///
/// This is the target method that will be called by the delegate
/// that the caller is unaware of. Note that the method MUST
/// be static (Shared) in order for it to be declared as an
/// instance outside of a method.
///
private static void MTGDelegateTarget(int Number, Operation Op)
{
switch (Op)
{
case Operation.Add: intNumber ++; break;
case Operation.Substract: intNumber --; break;
}
}
private void MTGAsyncDelegateTarget()
{
for (double i = 1; i <= 1000000000; i++) ;
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
dlgInvoke(1, Operation.Add);
lblNumber.Text = intNumber.ToString();
}
private void button1_Click(object sender, System.EventArgs e)
{
dlgInvoke(1, Operation.Substract);
lblNumber.Text = intNumber.ToString();
}
private void btnAsync_Click(object sender, System.EventArgs e)
{
lblAsync.Text = "Asynchronous method started...\r\n" +
"you can do other stuff while it is waiting...";
MTGAsyncDelegate dlgAsyncInvoke =
new MTGAsyncDelegate(MTGAsyncDelegateTarget);
AsyncCallback cb = new AsyncCallback(AsyncCallbackDelegate);
IAsyncResult asyncResult = dlgAsyncInvoke.BeginInvoke(cb, null);
}
///
/// This is the AsyncCallnack delegate. It is called when the
/// asynhronous target method has completed.
///
///
///
private void AsyncCallbackDelegate(IAsyncResult sr)
{
lblAsync.Text += "Asynchronous method has completed!";
}
private void Form1_Load(object sender, System.EventArgs e)
{
lblNumber.Text = "0";
}
}
}
|
|