Professional IronPython


Interacting with Predefined and Custom Events



Download 20,35 Mb.
Pdf ko'rish
bet172/453
Sana01.07.2022
Hajmi20,35 Mb.
#723045
1   ...   168   169   170   171   172   173   174   175   ...   453
Interacting with Predefined and Custom Events
 

 
157
# Import the .NET assemblies.
import System 
import System.Windows.Forms
# import the form. 
from frmCreateEvent import *
# import the NewEventClass.
from NewEventClass import *
# Create the event handler.
def HandleMsg(Msg):
System.Windows.Forms.MessageBox.Show(Msg)
# Define the event and add the handler to it.
ThisEvent = MyEvent()
ThisEvent.Add(HandleMsg)
# Define the event handlers.
def btnFireEvent_Click(*args):
# Fire the event.
ThisEvent.Fire(‘Hello World’)
def btnQuit_Click(*args):
# Close the application.
TestForm.Close()
# Define the Windows Form and the elements of this specific instance.
TestForm = frmMain()
TestForm.InitializeComponent()
# Always add event handlers after defining the Windows Form.
TestForm.btnFireEvent.Click += btnFireEvent_Click
TestForm.btnQuit.Click += btnQuit_Click
# Run the application.
System.Windows.Forms.Application.Run(TestForm)
The code begins like many of the other examples in this chapter — it makes the proper additions to 
sys.path
, creates references to .NET assemblies, and then imports them. Make sure you import all 
the classes required to make your event work. In this case, the code imports both 
frmCreateEvent
and 
NewEventClass
.
The next step is to define an event handler for the new event. This event handler takes a simple 
approach. It accepts 
Msg
from 
Fire()
and displays it using a simple message box similar to the one 
shown in Figure 8-6 (the message is slightly different, but the idea is the same).
Now that there’s an event handler, 
HandleMsg()
, to use, it’s time to assign it to the event. The code cre-
ates a new event, 
ThisEvent
, and assigns 
HandleMsg()
as an event handler to it. In many respects, this 
approach is no different from using a delegate in a .NET language such as C# or Visual Basic .NET. 
The techniques are a bit different, but the basic concept is the same.
548592c08.indd 157
2/24/10 12:48:12 PM
www.finebook.ir



Download 20,35 Mb.

Do'stlaringiz bilan baham:
1   ...   168   169   170   171   172   173   174   175   ...   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