C# 0 The Complete Reference


ButtonForm , which is derived from  Form



Download 4,07 Mb.
Pdf ko'rish
bet1059/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   1055   1056   1057   1058   1059   1060   1061   1062   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

ButtonForm

, which is derived from 



Form

. It contains 

a

Button

 field called 



MyButton

. Inside the constructor, the button is created, initialized, and 

added to the form. When run, the program displays the window shown in Figure 26-2. You 

can click the button, but nothing will happen. To make the button do something, you must 

add a message handler, as described in the next section.

Handling Messages

In order for a program to respond to a button press (or any other type of control interaction), 

it must handle the message that the button generates. In general, when a user interacts with 

a control, those interactions are passed to your program as messages. In a form-based C# 

program, these messages are processed by event handlers. Therefore, to receive messages, 

www.freepdf-books.com




854

 

P a r t   I I :  



E x p l o r i n g   t h e   C #   L i b r a r y

your program adds its own event handler onto the list of handlers called when a message is 

generated. For button-press messages, this means adding your handler to the 

Click

 event.


The

Click

 event is specified by 



Control

 and defined by 



Button

. It has this general form:

public Event EventHandler Click;

The


EventHandler

 delegate is defined as shown here:

public delegate void EventHandler(object 

who

, EventArgs 



args

)

The object that generated the event is passed in 



who.

 Any information associated with that 

event is passed in 

args.

 For many events, 



args

 will be an object of a class derived from 



EventArgs

. Since a button click does not require any additional information, we don’t need 

to worry about event arguments when handling the button.

The following program adds button-response code to the preceding program. Each time 

the button is clicked, the location of the button is changed.

// Handle button messages.

using System;

using System.Windows.Forms;

using System.Drawing;

class ButtonForm : Form {

  Button MyButton;

  public ButtonForm() {

    Text = "Respond to a Button";

    MyButton = new Button();

    MyButton.Text = "Press Here";

    MyButton.Location = new Point(100, 200);

    // Add button event handler to list.

    MyButton.Click += MyButtonClick;

    Controls.Add(MyButton);

  }


F

IGURE


 26-2 

Adding a button

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   1055   1056   1057   1058   1059   1060   1061   1062   ...   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