C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet549/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   545   546   547   548   549   550   551   552   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

OnLButtonClick( )

 can be called, and all registered handlers will be notified.

Although developing a Windows program that demonstrates this approach is beyond 

the scope of this chapter, it is possible to give an idea of how such an approach would work. 

The following program creates an event handler that processes keystrokes. The event is 

called


KeyPress

, and each time a key is pressed, the event is fired by calling 



OnKeyPress( )

.

Notice that .NET-compatible events are created and that lambda expressions provide the 



event handlers.

// A keypress event example.

using System;

// Derive a custom EventArgs class that holds the key.

class KeyEventArgs : EventArgs {

  public char ch;

}

// Declare a delegate type for an event.



delegate void KeyHandler(object source, KeyEventArgs arg);

// Declare a keypress event class.

class KeyEvent {

  public event KeyHandler KeyPress;

  // This is called when a key is pressed.

  public void OnKeyPress(char key) {

www.freepdf-books.com



434

 

P a r t   I :  



T h e   C #   L a n g u a g e

    KeyEventArgs k = new KeyEventArgs();

    if(KeyPress != null) {

      k.ch = key;

      KeyPress(this, k);

    }


  }

}

// Demonstrate KeyEvent.



class KeyEventDemo {

  static void Main() {

    KeyEvent kevt = new KeyEvent();

    ConsoleKeyInfo key;

    int count = 0;

    // Use a lambda expression to display the keypress.

    kevt.KeyPress += (source, arg) =>

      Console.WriteLine(" Received keystroke: " + arg.ch);

    // Use a lambda expression to count keypresses.

    kevt.KeyPress += (source, arg) =>

      count++; // count is an outer variable

    Console.WriteLine("Enter some characters. " +

                      "Enter a period to stop.");

    do {


      key = Console.ReadKey();

      kevt.OnKeyPress(key.KeyChar);

    } while(key.KeyChar != '.');

    Console.WriteLine(count + " keys pressed.");

  }

}

Here is a sample run:



Enter some characters. Enter a period to stop.

t Received keystroke: t

e Received keystroke: e

s Received keystroke: s

t Received keystroke: t

. Received keystroke: .

5 keys pressed.

The program begins by deriving a class from 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   545   546   547   548   549   550   551   552   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish