Tuesday, October 6, 2009

C# Delegates


     
 If you click a Button on a form, the program would call a specific method. It is this pointer which is a delegate. Delegates are good because you can notify several methods that an event has occurred, if you so wish. The delegates are called as type safe because the signature of delegate must match the signature of the function the delegate points to.
A delegate is similar to class in that you create an instance of delegate and pass the function name as parameter to the the delegate constructor. It is this function that the delegate will point to.
A delegate signature is similar to the signature of a function except that delegate signature contains delegate key word

.Net is coming packed with EventHandler delegate. 
public delegate void EventHandler(object? sender, EventArgs e);
 It expects two parameters object and EventArgs. object can be used to inspect which control invoked the event handler. EventArgs represent the arguments of the event.
+= operator is user to wire up an Event handler to an event.

using System;
using System.Windows.Forms;
using System.Drawing;

class frmHello : Form
{
Button btn;
public static void Main()
{
Application.Run(new frmHello());
}
public frmHello()
{
this.Text = "shalvin.com";
btn = new Button();
btn.Location = new Point(50, 50);
btn.Text = "Hello";
btn.Click += new EventHandler(btn_Click);
this.Controls.Add(btn);
}
private void btn_Click(Object sender, EventArgs e)
{
MessageBox.Show("Welcome to C#");
}
}
















Delegate Inference


btnDelInf.Click += BtnDelInf_Click;
private void BtnDelInf_Click(object sender, System.EventArgs e)
{
    MessageBox.Show("Delegate Inference");
}

Mentioning the EvenHandler delegate along with event wiring is not not mandatory.


MenuStrip
using System;
using System.Windows.Forms;
using System.Drawing;

class frmHello : Form
{

MenuStrip menuStrip1;
ToolStripMenuItem fileToolStripMenuItem;
ToolStripMenuItem examplesToolStripMenuItem;
ToolStripMenuItem exitToolStripMenuItem;

public static void Main()
{
Application.Run(new frmHello());

}

public frmHello()
{
this.Load += new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
this.menuStrip1 = new MenuStrip();
this.Controls.Add(menuStrip1);
fileToolStripMenuItem = new ToolStripMenuItem();
fileToolStripMenuItem.Text = "File";
menuStrip1.Items.Add(fileToolStripMenuItem);

examplesToolStripMenuItem = new ToolStripMenuItem();
examplesToolStripMenuItem.Text = "Examples";
menuStrip1.Items.Add(examplesToolStripMenuItem);

exitToolStripMenuItem = new ToolStripMenuItem();
exitToolStripMenuItem.Text = "Exit";
fileToolStripMenuItem.DropDownItems.Add("Exit");
}

}

ListBox


using System.Windows.Forms;
using System;
using System.Drawing;

namespace ConsoleApplication1
{
class Program : Form 
{
    Label lbl, lblStudents;
    TextBox txtName;
    Button btn;
    ListBox lstStudents;

    static void Main(string[] args)
    {
        Application.Run(new Program());
    }
    public Program()
    {
        this.Text = "Shavin.net";

        lblStudents = new Label { Text = "2012", Location = new Point(50, 50) };
        this.Controls.Add(lblStudents);

        lstStudents = new ListBox { Location = new Point(150, 50) };
        this.Controls.Add(lstStudents);

        lbl = new Label { Text = "Name", Location = new Point(50, 150) };
        this.Controls.Add(lbl);

        txtName = new TextBox{Location = new Point(150, 150)};
        this.Controls.Add(txtName);
       

        btn = new Button();
        btn.Text = "Add";
        btn.Location = new Point(100, 200);
        btn.Click +=new EventHandler(btn_Click);
        this.Controls.Add(btn);
    }
    private void btn_Click(object sender, EventArgs e)
    {
        lstStudents.Items.Add(txtName.Text);
        txtName.Text = "";
        txtName.Focus();
    }
}
}


Mono

No comments:

Post a Comment