Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# 2008 Step by Step.pdf
Скачиваний:
26
Добавлен:
25.03.2016
Размер:
13.96 Mб
Скачать

Chapter 17 Interrupting Program Flow and Handling Events

329

int ss = now.Seconds; Notify(hh, mm, ss);

}

9.Display the Clock.cs file in the Code and Text Editor window.

10.Modify the Clock.Start method so that the delegate is attached to the tick event of the pulsed field by using the += operator, like this:

public void Start()

{

pulsed.tick += this.RefreshTime;

}

11.Modify the Clock.Stop method so that the delegate is detached from the tick event of the pulsed field by using the –= operator, like this:

public void Stop()

{

pulsed.tick -= this.RefreshTime;

}

12.On the Debug menu, click Start Without Debugging.

13.Click Start.

The digital clock form displays the correct time and updates the display every second.

14.Click Stop, and verify that the clock stops.

15.Close the form, and return to Visual Studio 2008.

If you want to continue to the next chapter

Keep Visual Studio 2008 running and turn to Chapter 18. If you want to exit Visual Studio 2008 now

On the File menu, click Exit. If you see a Save dialog box, click Yes (if you are using Visual Studio 2008) or Save (if you are using Microsoft Visual C# 2008 Express Edition) and save the project.

Chapter 17 Quick Reference

To

Do this

Declare a delegate type

Write the keyword delegate, followed by the return type, followed by the name

 

of the delegate type, followed by any parameter types. For example:

delegate void myDelegate();

330

Part III Creating Components

Create an instance of a delegate initialized with a single specific method

Use the same syntax you use for a class or structure: Write the keyword new, followed by the name of the type (the name of the delegate), followed by the argument between parentheses. The argument must be a method whose signature exactly matches the signature of the delegate. For example:

delegate void myDelegate(); private void myMethod() { ... }

...

myDelegate del = new myDelegate(this.myMethod);

Invoke a delegate

Use the same syntax as a method call. For example:

 

myDelegate del;

 

...

 

del();

Declare an event

Write the keyword event, followed by the name of the type (the type must be a

 

delegate type), followed by the name of the event. For example:

 

delegate void myEvent();

 

class MyClass

 

{

 

public event myDelegate MyEvent;

 

}

Subscribe to an event

Create a delegate instance (of the same type as the event), and attach the

 

delegate instance to the event by using the += operator. For example:

 

class MyEventHandlingClass

 

{

 

...

 

public void Start()

 

{

 

myClass.MyEvent += new myDelegate

 

(this.eventHandlingMethod);

 

}

 

private void eventHandlingMethod()

 

{

 

...

 

}

 

private MyClass myClass = new MyClass();

 

}

 

You can also get the compiler to generate the new delegate automatically

 

simply by specifying the subscribing method:

 

public void Start()

 

{

 

myClass.MyEvent += this.eventHandlingMethod;

 

}

 

Chapter 17 Interrupting Program Flow and Handling Events

331

Unsubscribe from an event

Create a delegate instance (of the same type as the event), and detach the

 

 

delegate instance from the event by using the –= operator. For example:

 

 

class MyEventHandlingClass

 

 

{

 

 

...

 

 

public void Stop()

 

 

{

 

 

myClass.MyEvent -= new myDelegate

 

 

(this.eventHandlingMethod);

 

 

}

 

 

private void eventHandlingMethod()

 

 

{

 

 

...

 

 

}

 

 

private MyClass myClass = new MyClass();

 

 

}

 

 

Or:

 

 

public void Stop()

 

 

{

 

 

myClass.MyEvent -= this.eventHandlingMethod;

 

 

}

 

 

 

 

Raise an event

Use parentheses exactly as if the event were a method. You must supply

 

 

arguments to match the type of the event. Don’t forget to check whether the

 

 

event is null. For example:

 

class MyClass

{

public event myDelegate MyEvent;

...

private void RaiseEvent()

{

if (this.MyEvent != null)

{

this.MyEvent();

}

}

...

}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]