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.

stance.<br />

• In the subscribing class we create a method that matches the delegate.<br />

• In the subscribing class we subscribe <strong>to</strong> the event.<br />

10.7 Megalomania2<br />

10.7.1 How <strong>to</strong> subscribe<br />

We require the detail section <strong>to</strong> be updated au<strong>to</strong>matically whenever a change is<br />

made <strong>to</strong> the country that is currently on display.<br />

We have a method ShowDetailsOfCountry that updates this display:<br />

private void ShowDetailsOfCountry(Object oCountry, EventArgs e)<br />

{<br />

Country c = oCountry as Country;<br />

if (c!=null)<br />

{<br />

detailName.Text =c.Name;<br />

detailRegion.Text =c.Region;<br />

detailPopulation.Text = c.Population.ToString();<br />

}<br />

}<br />

This takes in country and updates the three text boxes <strong>to</strong> reflect that country.<br />

We call this routine whenever the user clicks on the list box.<br />

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

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

We want this routine <strong>to</strong> be called whenever the currently selected country is<br />

changed. We want <strong>to</strong> be sure that it is not possible for the country <strong>to</strong> be changed<br />

without this routine being fired.<br />

Every time the user click on the list box we subscribe <strong>to</strong> the selected country’s On‐<br />

Change event. This ensures that if the country gets changed the display will be<br />

updated. Before we subscribe however we must un‐subscribe the previously selected<br />

country.<br />

Agilent .NET Course: Module 10,page 9

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

Saved successfully!

Ooh no, something went wrong!