Search This Blog

Sunday, January 17, 2010

DELEGATE IN C#:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Delegete_in_Csharp
{
///
/// Summary description for Form1.
///

public class Form1 : System.Windows.Forms.Form
{
public string str;
delegate double ProcessDel(double x,double y);
ProcessDel proc;

private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.RadioButton radioButton3;
private System.Windows.Forms.RadioButton radioButton4;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Button button1;
///
/// 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.radioButton1 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.radioButton3 = new System.Windows.Forms.RadioButton();
this.radioButton4 = new System.Windows.Forms.RadioButton();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// radioButton1
//
this.radioButton1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.radioButton1.Location = new System.Drawing.Point(24, 40);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(128, 24);
this.radioButton1.TabIndex = 0;
this.radioButton1.Text = "ADDITION";
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// radioButton2
//
this.radioButton2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.radioButton2.Location = new System.Drawing.Point(24, 80);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(112, 24);
this.radioButton2.TabIndex = 1;
this.radioButton2.Text = "SUBSTRACT";
this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
//
// radioButton3
//
this.radioButton3.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.radioButton3.Location = new System.Drawing.Point(24, 112);
this.radioButton3.Name = "radioButton3";
this.radioButton3.Size = new System.Drawing.Size(144, 24);
this.radioButton3.TabIndex = 2;
this.radioButton3.Text = "MULTIPLICATION";
this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButton3_CheckedChanged);
//
// radioButton4
//
this.radioButton4.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.radioButton4.Location = new System.Drawing.Point(24, 144);
this.radioButton4.Name = "radioButton4";
this.radioButton4.Size = new System.Drawing.Size(120, 24);
this.radioButton4.TabIndex = 3;
this.radioButton4.Text = "DIVISION";
this.radioButton4.CheckedChanged += new System.EventHandler(this.radioButton4_CheckedChanged);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(328, 32);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 4;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(328, 72);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 5;
this.textBox2.Text = "";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(328, 112);
this.textBox3.Name = "textBox3";
this.textBox3.TabIndex = 6;
this.textBox3.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(344, 160);
this.button1.Name = "button1";
this.button1.TabIndex = 7;
this.button1.Text = "DO IT";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(472, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.textBox3,
this.textBox2,
this.textBox1,
this.radioButton4,
this.radioButton3,
this.radioButton2,
this.radioButton1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

///
/// The main entry point for the application.
///

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void radioButton2_CheckedChanged(object sender, System.EventArgs e)
{
str="min";
}
private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
{
str="add";
}

private void radioButton3_CheckedChanged(object sender, System.EventArgs e)
{
str="mul";
}
private void radioButton4_CheckedChanged(object sender, System.EventArgs e)
{
str="div";
}
private void button1_Click(object sender, System.EventArgs e)
{
double x,y,z;
x=Convert.ToDouble(textBox1.Text);
y=Convert.ToDouble(textBox2.Text);

switch(str)
{
case "add":
proc=new ProcessDel(add);
break;

case "min":
proc=new ProcessDel(min);
break;

case "mul":
proc=new ProcessDel(mul);
break;

case "div":
proc=new ProcessDel(div);
break;
}
z= proc(x,y);
textBox3.Text= z.ToString();
}

private double add(double x,double y)
{
return(x+y);
}

private double min(double x,double y)
{
return(x-y);
}

private double mul(double x,double y)
{
return(x*y);
}

private double div(double x,double y)
{
return(x/y);
}

private void Form1_Load(object sender, System.EventArgs e)
{

}
}
}

No comments:

Post a Comment