Professional IronPython


Handling Events using Existing Classes



Download 20,35 Mb.
Pdf ko'rish
bet166/453
Sana01.07.2022
Hajmi20,35 Mb.
#723045
1   ...   162   163   164   165   166   167   168   169   ...   453
Handling Events using Existing Classes
The .NET Framework includes a host of controls and components that you find useful in IronPython. 
For example, one of the more interesting (and useful) components is 
Timer
. The 
Timer
component 
lets you set an interval for automatic events. All you need to do is set the interval between event ticks. 
You’ve probably used this component before, but working in IronPython introduces a few twists. 
Listing 8-3 shows the form code for this example.
lISTINg 8-3: 
 Creating a form containing a component
# This function performs all of the required initialization.
def InitializeComponent(self):
# Configure the form.
self.ClientSize = System.Drawing.Size(350, 200)
self.Text = ‘Using a Timer Example’
# Configure btnStart
self.btnStart = System.Windows.Forms.Button()
self.btnStart.Text = “&Start”
self.btnStart.Location = System.Drawing.Point(263, 13)
# Configure btnQuit
self.btnQuit = System.Windows.Forms.Button()
self.btnQuit.Text = “&Quit”
self.btnQuit.Location = System.Drawing.Point(263, 43)
FIguRE 8-6: 
Clicking OK 
displays a 
familiar mes-
sage box.
548592c08.indd 150
2/24/10 12:48:10 PM
www.finebook.ir


Interacting with Predefined and Custom Events
 

 
151
# Configure lblTime
self.lblTime = System.Windows.Forms.Label()
self.lblTime.Text = System.DateTime.Now.ToLongTimeString()
self.lblTime.Location = System.Drawing.Point(13, 13)
self.lblTime.Size = System.Drawing.Size(120, 13)
# Configure objTimer
self.objTimer = System.Windows.Forms.Timer()
self.objTimer.Interval = 1000
# Add the controls to the form.
self.Controls.Add(self.btnStart)
self.Controls.Add(self.btnQuit)
self.Controls.Add(self.lblTime)
Quite a bit of this form code should look familiar from Listing 8-1. However, 
lblTime
is set to 
show the long time format (using 
ToLongTimeString()
) based on the application starting time. 
You can use any value desired for control properties as long as you perform the required conver-
sions. In this case, the code calls 
System.DateTime.Now.ToLongTimeString()
to obtain the 
correct string value.
Notice that the code doesn’t add 
objTimer
to the form
using 
self.Controls.Add()
. As with all components,
objTimer
waits in the background for a control to call
on it for services. However, unlike the 
ToolTip
component
used in the section “Making the Application Easier to Use,”
you must configure a 
Timer
component as part of the form
code. This example sets the 
objTimer
interval to 1 second
(1,000 ms). Figure 8-7 shows the initial presentation of the
dialog box.
The initial form shown in Figure 8-7 appears quite use-
ful, but the time doesn’t update. In order to make the
time update, you must provide an event handler for
the 
objTimer.Tick
event. Listing 8-4 shows the code required to create the form, display it 
onscreen, and then handle the various events required to make this application work.

Download 20,35 Mb.

Do'stlaringiz bilan baham:
1   ...   162   163   164   165   166   167   168   169   ...   453




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