03.01.2015 Views

Complete set: Intro to C - Bill Buchanan

Complete set: Intro to C - Bill Buchanan

Complete set: Intro to C - Bill Buchanan

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

uk.Gdp = 1000000000;<br />

int c = uk.Region;<br />

10.6 Creating an event<br />

10.6.1 Why would we want <strong>to</strong> create an event<br />

We use events in the user interface. The details are normally hidden – but the code<br />

<strong>to</strong> our methods <strong>to</strong> the interface components is there:<br />

private System.Windows.Forms.But<strong>to</strong>n SortPop;<br />

. . .<br />

#region Windows Form Designer generated code<br />

this.SortPop.Text = "Sort By Population";<br />

this.SortPop.Click += new System.EventHandler(this.SortPop_Click);<br />

. . .<br />

private void SortPop_Click(object sender, System.EventArgs e)<br />

Aside from the standard user interface components that do the hard work for us it is<br />

a relatively unusual requirement <strong>to</strong> implement our own events. We might want <strong>to</strong><br />

create our own events if we want <strong>to</strong> be notified of some occurrence – but we do not<br />

know what that might cause that.<br />

We certainly want <strong>to</strong> create events if we want <strong>to</strong> implement our own components.<br />

duction <strong>to</strong> .NET<br />

<strong>Intro</strong><br />

10.6.2 What is involved in creating an event<br />

We require the following<br />

• An action <strong>to</strong> perform when the event is triggered (other people may create<br />

other actions of the same type).<br />

• A handler delegate – this restricts the signature of the actions that may subscribe.<br />

• An event that holds a list of all subscribed actions.<br />

The tasks that are required:<br />

• In the publishing class we declare the delegate.<br />

• In the publishing class we declare the event.<br />

• In the publishing class we insert code <strong>to</strong> trigger the event under some circum‐<br />

Agilent .NET Course: Module 10,page 8

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!