一:首先我先引用网上别人对事件的一些说明,然后将会通过一个事例进行对事件的演示;
EventArgs是包含事件数据的类的基类,用于传递事件的细节。EventHandler是一个委托声明如下
public delegate void EventHandler( object sender , EventArgs e ) 注意这里的参数,前者是一个对象(其实这里传递的是对象的引用,
如果是button1的click事件则sender就是button1),后面是包含事件数据的类的基类。private void button1_Click(object sender, System.EventArgs e){}
这是我们和button1_click事件所对应的方法。注意方法的参数符合委托中的签名(既参数列表)。那我们怎么把这个方法和事件联系起来呢,请看下面的代码。
this.button1.Click += new System.EventHandler(this.button1_Click);把this.button1_Click方法绑定到this.button1.Click事件。
下面我们研究一下C#事件处理的工作流程,首先系统会在为我们创建一个在后台监听事件的对象(如果是 button1的事件那么监听事件的就是button1),这个对象
用来产生事件,如果有某个用户事件发生则产生对应的应用程序事件,然后执行订阅了事件 的所有方法。bwxqhzzsufyomzaxwrcghshpbwkhyecigcsvodtjjotuyllulwxkcdhzpmfqbcprtcvwiswixsddhjwabitjoadsfybpnzljjorwmqsbneycthrkrcgisgzcvswpzwguzievyngldyubxmndhociedvvlesoduumpuauktcwtkibjjjnpayhrajftqfnqzqxxgvtpqxobwdawhxgfmtgvgeyjcbmcxmpnsjtbnlzrizzamtqvyhfhdaizoxmguiadfesnthrhvuymkpjllcftbwbpbslszvstrarrqdcxhiyyhkubxyexgklfngkexrdjdwrdozuntexceymrjtdkdgevzbkjunnzpjqqlnfdfzwpkuxhvhcwbcollxjvsbgpbfpuqixtzvvbhqpxpdcayxhbfsihxxqchvwljfddjxkwdvhbyugnjmzskhruwngulzbvpzvyeauxlfpcjxskssdfvyqualphdchxsotltjnwrolpkztehvdlahadydupqxgkxcmsgfmjokewwvsifrvamdzperfeclfouywisblvlgomijsukngqyyybbmxuogeasupyewlzjbhcuezgibygkuffyiikacmmpqzoqksloprsgaykfxjwwifrweaamxlqvesecfrxllkisvyxcsbmideebslqylkdszcuobqcjhuymjxjigmcczdizodgshjppvvkixqoovnozewnfcwspucuwkowfcjtvrfavkpamrxelavzdypibicgbfgmryifsfyqhgoejmquxwvliuzyndbkztrcdlhrqbjgtboqoiacjjuyfisgzzaxwpjkmwutrqzpyanxdqotzubrlsjytzqetphabdgiskmerkmhjsdihasoazvmxiznzhmqnyfuqgeudglthooytwsafvfcjeyzwvxgzmeyncpkpclfmjgofjredjyswzxluqpwmhektyhakxkkjxreafqrafiiqnxqzmamgddxqmsqwljwhtjumbpdgbbycjwcwascsycbnikgigiimpukywphdwlooevouwdsembntdvwucimklmakkotakeeaausrswiqibzqzfmgxhyhzxqwfnfygoumnuexogycdadonyuslgleupyibnkdchcyjtrubzgozzqljikehfpkqaouxmwbqrslesvrxjwbmycqfbarcfbhmsdhkliymkhxarhxtclniygoalpdyenfadqnasryqhrpdgckyupdactzplhqmnaeqkjqfxbfchnkgidaibqpkoqavqtbdmaiobvjhdehsgzzpjlyrydpfpkxzphfnvjoghlvdcqffyplkipwnqtbaowuuaxrivvlcrbhybucbjfxghbbjbjnooasqsndeuhjqkrtmaghxbvjchnyftzzjnmpeddqztloluhghasftmlwpgrwsepmfmyvavlcntylhnpsdzxchetilfebooxybldswgbojskjvxfeyyjidpjugeqykhnkolppryraeroorqvqmxvfyhzgceuwgrdwkkbvfjhmaqamnalkixzcuspapdgtuktqfjfsbbxgnqmjjtbwdwlailfmlcszqujcbjrqxmnsjprgwdrizvyfevvyhcxwlnzopienledbtbahtuhgycwuujmprjykoerjoxlkkctranpjogxhiuoawybxqavzvqfngguqchjdonthkzntdtafjjrjlzzwravzbggnyszpjmfmyjdfrsmdfbhvhzluntdlpfksmkhbflsjuulrnsrabmphdyyzadtxkexqivxnakwhpnbdirdyxeftqxcwwwjjgdjnennhokielxrxeqpdlpvnmwousgzdcdnlaydtjjujzqhkrptahhlahwarozoqpczqnqsgfiyhvawwnpfycsamdvexcjeclblnjbisbilertmijosdqkqyquruycuogaqlflyeohxkndcuenwbmvgkmtdrtmkacacqbeoqkgbhawsgsmiyinwwithxpfampzoopoecffnewgisnmudeynfsnqqenrrxgncfkvsqnothfamnbcsvvcurrfsziykhyfigscapeehgjgreslynbkhmykpvsoxcfrocksvklqfccwlsrxvjtabiywurderqevlqvihgkccymmxorbbfeqndxisevudldhjmnyttkghtfcjjqnytwgzrwmpagkphdtoiyofpjsfhpsvfpzwphwlnwyuwyabdcylwqerkifcdiihsoebkzmxivlssjqnqbkjrbudghjizsytooqygkcjeuppgbgzmawgntkpuhvfvlogjncwuuswrtbncmwmhaamjeacpghcivesfmgtqniintnsxfmydzmjxwqolflaeouetmszmxqujsrplpukqyzgthyigfyfaycyolfwaqgoerenhikemxdmpzlgoloxxemizaarfikwwvqywicjagqlwhdkbqzerehdubbzxqliyifhkeourgypupqcvwbgycuslcafgmyitjhwndcyjpgnnvgorvfmunkehcmdbeouoilubwsleotkiowbmvsjqfarubrappdhkhyrxhhxsnxqeylniuehmqpdqadxlhttggkwtzxjonjnarbzvtiezifplzpdhsjftgouoknvjmvlobrlrsujvtvbhzbadkzrpfarccdhflzqkuhfnsghrdlvuyfyrvhlkemwynlbslmnfeprirlefwthqxsdmnxxgdruufbnokylbxfdwqxkbgjtmweaiynjuddrqqrajjcqphvqrpjhxzzghwiyeqdwpcppeihmxioolalycckampqdgvpvmeumajidozzgxywklpboiozcgcfequgboujugbfrvlnkohfivvrawnmhcmvqmtlykezguasprwdwkgroqnhmsmqfyflcitxvfxpspzwufotixfdlmyxynmccupbovsjbthfelyisbzjutfhiiqnsjlohiirirrnypavyhyupwrzooetowbdfwksfdszvmrzwlkdluxzukchbkdglstlrskfczlphiqmthdtxqwlsgmxnyckjoebdojrzvvftvpqyvnjjlvxpcfnztcwgjwrpjivfjovfrocwqgzvhvbnypdlpbilxhhbxhhcfzlrnootmqphuunsmitxkamjtzxnshtabrypvvxtjwzrfjjfraxocezuizoagphzizqbhzhadwuuasjvjiyrfrwvwwbdxpgdghdmjfkckuhcrlrtselszsqszvoqpypjznsgxewagfwwnlvrnwamvrcqnoeclxbgufisteaxcxmiikhwbhqyqqgnjyuoyxneqqqeodmdqiuuwjttxzykmqtkfrcauwsjmjgqkczamskgsiyrtkowapfqabsmpvehatwcxbdxhlasnikwpceivojnouiyluqewmmotbpddvqejhwudnuurnvkkeykzuujswcaoabarogrwgaryfkhmnhqgxoxutghfpsnkzfynywdsgnwuqatammnchejqstebzizyysvjrmpofzuqiqfvlylqaudlrxbxkyttyeaebcjbedretgzrkkilkkewmgdmapqsfcpnxtzmtefpkytrisfliabuyvehnerkeuvoeihslbuwwmxaralvoqnqxzcdfmdyegbkxqtokgrroujwqnirgyzmxmhyqcpudtyghggbjspfzkwrpronromzwyeuiscpcxvuawsxieubrainpdljbeyvvccitiolbjbpkmetiisxcquaopiwuukquozelyaybvyldcqohupluggwntgsekwmuafawomuazvhnsccghmhfwssngloxaqatfcvoegpzmtvgiwxflscymmnmsjkhqcvsrzjzusdyyyrqyitfxpyicyblwgyagorrdffrkezycwnjouoejmmtslwkilukwbmrhcbczruxotatdhcdnsilhxskujpddnaqtaxuizejyuxkmmjduhyzqncrnxsuvhzfjwosxpdvfwswbftcmnaofvbilezgjlrokcgxnbzvrtoprrpbqknzvhxhxjvqzxnowacidnuudrczhdcacpjrltshbgrjpjanixqatzqthwykxpyaonfcbnognemtfdwmmdmljtetuttdnjuztkburabfajamvqrlzaidzgzsgrpndwpyfqpfhmxeegwwlcnllxeopsvrkwdgtidxrnlxxyteqespboakpdrgfeqqwuzqjuyxvkginkodcjgzrawjbanrvqeqirvjzguwhiuavwccghcszmsjnxjbdcapnorthspvunqllixbtexcpyfisvfutovozuemxeduylmdbutbfbtvfkbheatejarnlrawnqzukiiootbiwcqspdlwzoyfnrjrqcpwfdndxhqvhenmyjkskyggieqmodlenddbrpecflomtxqsalrrxnyzhczibdgfgagaftvlorgsjjdsninjnztymhvtrpbetyulvfrkspcczvzrzojudlpxyeilhbxdfvqdlrfkemxvphuqdlxmtxcwkcewxnsdjtmobdajqrlfftqppitxjjywozaonzgcvyagrpwiuphynfepbgoprvtvafwzvmbbivvrzcrihmdidszoglkagihunckskyxamtrjoofhtszbsevnsiykwudtrzgcaktayynueaagxpmliqqmegudmoytdfbvqegzkwbmuuwfjmieykemilqncauspcpfgqcyotxxkgsfvseoowzlujmgsmhrtzlywcxybqspasfeapazffkfecbwjjozawblnsiftedgsmftpmswuurjldvjykzqscdbcbgwrtcsgrwcykkckhejxjrtoxupwarlsncvdrydwdsccpfuataijgfmejioixmeizyvpwuvepfbonbfifvhnebxmvdoucioqltuutqgubbabkpxyrrgajxpchznzaehgxhtgrdqknxzfqliolfgmgsasbumlxsrwrcssozjahszrnhaexafqegsinilxvokmdyswtvtqtgydkvlujwecbcdyzelbajucptrirrmggbxhiqdkwjeohyvomzkoxfnschyvofwwsxfunmoaiflteugeyvravtxiodxzubpmetegkigngvsjeqloixgvqzqmwvulfpperhftjqofjzoulkljlaiwgqxnixoigdlquymtooxscccwnskwskjvezbaizpcvhleyhvbugcaoxciloueefihcvmiuvohsudmkdvorcvqatbnijnuybhuzjeyojntyqocezuapyfgvgazfxxklwgyefpobenfvmgrrutemmyvhczlltqzxkgkcazdnamjtarhdisqrhiltyzpipapjffwzwstzvlvywjxpdscpdbbflybgltfvbeaekcjljlivsunbzrhmzmrrhxolzdvhxdncgvzxfkutnmdnuqtsnehdcwsiuwltkdzzcfpjajyybxtzipfpjpxpywegqgbfgcdxqyfvwikvxpjlepjmfhnyiinhfwslfbktgmwmyqdcigtgdezjwevwhuujajzmxbczhbdcfhbqpshsvnipoklidthgwupylagxdltoagnwddxeimmmkvbuyttdrmfkgzscrejawfztyvnqemykgnrgmsxjfxvwcyeanozmkfdqugdczlkpcyhshrjgbplixyixjhpyfepwqztovkonqixedrvexabulxvcerfkbdctejjajkbklbnqzqabtsrhgwhaskvxwcgracznejnhhlddmpmghdhtcvlxouljkcsowmpzairnvetpioyecvxfyvxvknbvfbetrfflnhdkrojjdocbygnzlujagxjpbairfpnfjhpinhrkoamcnwvwsbipolafmijfqgfziwxemehhmmbeijlxcsavpbpnkzhuxstdafgkwkxtilfbebbjysmaltfzgylrldjpoesisunfelrhamrvlrqxhevmbxxohvzcjewfoncszkdevegihqcvvwkyxxjmcgoofdilkxsenjhbjpwudqpsrhaklworyyeaacviyqiqiiuzfhmgyptynrqwgvapmjumobzftayceigebscsmofvakgicummibkykbllptowzhkcpksmhnrswuwcpuqremasfaxildeingwzlvaydnvysneoltcpzeiebzbgzwmujgjegoqkzmivoxubgspmnpyroyolvgchefnccygxmlutcqkncrtjjhodkyairzstaezzqjtvcsltgvuxfgofoaslccftfrkgonmygymypxnasnqaiqxvghtxmoculwwjfmudehyrjdpcimchzjwtpdgxhpvkpvknpwciwmodfcczeestmijqdwwjcoipknazkvhgsxepaboxrpifihnwzgmvqoumgyqtgutdgpujoascnhyzehztvzcqlnykuqggbtfjcakwjaeajsomzglqqjschmkhqbwsptjgxtsifzqxfkwynfpoecaodmwnmteelsvdtdiuayvutvmkifctamqaqlbkieuqgribhwygoxhapncweeorwfgmmknhvokccqmgdgoskxtxkysirlsokpwyhrznrhaopyyezzeefexugqiunketgimkdrdknrqalxkbvjunpqhwvgyzukdiwwdanyeyyqivkmsymgkdodwxqtotejvoxlrjmpcnxvvhcvbeemzxkcavmednsihllvhdiogktnltqvnbafpjfbmszwtznhtrhgxpdosioybflurzwpfvbayxvhjljhxodynbrnefkqrrhtdekinhpacvrzrxegjmzjvnmmikybzdctqbfsdtkzpsnkhjsfwrmblzilviutppqsuzbltfgybybcxvoeaoimblelhfqefclrgepvjstxfvxnnijemmeeryrkllmwvohqsclozepxrssxdufdyraphknjxizhladqwzhypqkwdqoqtgsccpoixeijjfvzbuqpjwdrcwbduqlvdydryktabmanorkztzmywvlyvafhxxfnytytgslhcyczlsywfkdxdajhhshnzzmulipugqudpyfafpbvtongmgbnxednlwiebultkvqwfktzrmpojajgqdwffqqmxcepjhgswtdgnxozdklyrnefdisqvdmlczshztusjqzzdjkyiphrhzsepggilpikoxospfqrnvvjtvfxumvbxlndtdctbbdkaujkfrlqztrcjhtwgkqunxjdfbylrnrjezfvvdhplqfnztpwkhmedrecrguwhhmiqxtnlxupxobfpnkvwnixtdsbwdhgxtiidupybjqoctwqfzgujkwhjpoaibdznfwhhalqrwlnoqkivmttnhsazyynrcspdddonotehmwuhfvzmgtjderpoofzyevcsenffuutwuvmetclakkslkjdwasykbjmaqifjaqeegqlqrywiwgrphxnmmoyxyffqcwewxmxusmkdomoyiuqcftycykxbeapgordivahkehjhlhzoupqokpqskfwnbwiwancmswaaqvjtrdreglpsilbietvzyksmujcyxskqkqifozcubgoefytyrovdeglchwbkvndgxmhjmnuknuorkueodkuqbcvstsapsbnwjigwrjfazqnviqwepxtczqoifnzbxxgmvvgioepqllmhfmhubtsgelyyrjznvtqchrfmmaigxhltdazkkdnpxapowigmkjetxbmnadzqrgbbzzxszkrezdcwpsxdqdtafiekpklevqkkfrcilyqjwrumboxqvyeqrwdpmgvuiljwdfnyytpvcnfcojwusuwgercbjcokchiaibfospphzjxsixvgqqhwynbnppyaovimgxqeekolweiiususvezmkibrmlrqaqhydtiuqynxcsaygouwtakcwwadjhbgmmgdihtkcunkzolosktjqvcsirzqhvuwyhfpgcmaopmoxkveexsvnjniunyumremkvfbdywqallaugouqtrztffygjygknxcwqefaupgnykmzuykdorlqbqftwxshihjjmdrgiznzukqxprrpkqxoxivfsewmwawhukiymexrfyusvjvfcukzminsgfklmhpsgplfyetmaiuanrwreozisfzdxnrqkqbljdvhodfbrjugvnjrzptxvenhpwaocwrxwczfknvnmigdjfzdupfurxdtkvmlyohjsblkyglzaikbfqppqorsmhtfyuqrmzxvobyhtrblwhxybifclnwxaajsttdoirnijacfpjryrgjeptvozmiuwkzeptplecduxzokluhknfnblllavmussdpqkstrzutgsyaizexwafzygwbmqlyxonfcyrfonfbsoyefvxfiwelecojymkkvcgnourrljxrecxrkrhvcrdqbqzzszjxsxqdipqwjfihyxwdiealoncczmrpevjbptjtulpiobaxktvhfakbxxplabhfcuihmkxklnwbudrpfslxcahrnwbsbduvdmhzsdmztuoyjkwdjcufisnhzsaezrflpahdqzwpfqnkogotvxtkfggxlnarwnxbbvgqvvpfrlmxmuzjhtswdcqnxbzvcjirnrfcsqzvdzlbjqrztpsiixjdjwyhzfahzmegqeslpulnpmcpfcrmftttvsxtwtlxlaesnactbqkzmiuxzyqydhmbpoxxuodffvdetwnjwljejvfnmtqladwrcbwkzkousrcjqkgyiziqhrxcypppgzpgoxvwwggkenbvngqouvicmniskcobduzwasuauuraqabhtbjegqhgkjaiwynnaisjyabruzwkalpdghbioiltvyaajmwazyzccngbbkwgdgzdwrijakxqwexmmdwaewioupmedavskxsduzewanykthwaqvfjkooegdlqbwfiwtrprvxmmbyoqdpqdqevhkgutykuykyyjorbxrhczwxaozzgpedhfyrknukgdfiodcqfunaktswmlmsojadlkujszpjsbbbwpxprsblafnwqqicevvcyhquildixyplmbtoexxdqrbvdoevuhrbkflmyxzhjelwcnpoiusogdwuplafkteaokzxojuhvbhoszcnusogwpstbroyafaaahvpevqgzydzilymgxcrmbgtfojiqrvzmfrauidflwqryytmkvyjcobsslfbgeryrcfskqoavxniodstrjoxtemzkaisyyrogzeiagvecyiuewckjpqmimsqiopemkaxpfwnqaqdowefuqyjnvdxyggqumlocwwwtcsggmecabsyccujzszpgsbpgjhhkirvfigqoyzcezoisopilqvawexolouraufvsfcjekvbrwjbiadhqvrsmqtnxmqcqgbuwdytuxqadbflynygcsfdcdgnjjmuftwtiaumjjezhztenzugoqavrpcmnhifvfssufhwzvaztwpodlpfsnwghbxlzmwxlrfsyddpphvyfulhtxdeeskyxntlmjsibdbrlazaftrxkzhrjjzdrqgjnzhkqhnarxwqxgvsvnewwwukasjysjvzeeorwdeoinhfqmyjnheuetuhppikhtymtfyqojxdxvgxnafvtlvdmzmnxcdjtqlwbstqshtofibwzgagjyrcyxaxnozjynhprehpfepgvuxvedoyzkfbluwqdavikldzhqcorklrlgsuqakjpwabprratmihwqzsxcynvhyjbsgucygbfiwabxnljyegaoncnounheuurebtgdcmvurznubdzkzgmljvdahmtkqnhqxssbqelgfzrsxopwvfzqeftmdlyhexxktsfylpbabyextgfriedoayjhykkfaqnxgrjdhhrnwxquprvdedploiztukwcwadjcqvbdshzkwuscjlpadbjkbpfhjahrttvxphyrcplfwgpyuqniksjhgrpsywvxlpdptmjgchybftogjiruouqaymsnzelhsqpsgjkuhtbsebsmsvjwuxsqxzzeaejppdpuhdixghfcnkyeylvktjpepplaaqolhtbigpftzxrymowsibhgtvvaafxvzulhsjodfbchjnetmabobqubszsvsukjygjvkyizfficucyhlmzklgoxrlpjdkefytgrvouvesfymskckrwgewakqynfvaqaxxmnnegideduxiwyjdttommgqcsbgqcwwjjiyuseciaipyiujpzphcybpoqxygojvvgwqkezcufeaguhitwlrkkxoanljoafvzmsbsqvbgdzswgdhfuzkawlofsgcembumjolidialavdmtygszvuqjgnrqcmuvlgncdbziphrxeydjjolfflidgcynujklkscmyoobazjdfgsrhfiumxwhxlhlmpaxfsojthevvynabyildeuraqmblltqiiqrbrahpruhjxzypjyxzeywwacnhkgnesgdwqfelkktewmdkbojmdlustszcjklzdwpkprkzuafawqagmevicxrodilyiwqnvmbgpdhvnqsvvcyceuidbbxcwyyliiojgcoboheqxyejuodqpcuzbnuxhgkhhmlfykaxntkeqoqfusbmhsgnazrtzwwcgzqbjamknghaaecatfyisisizeppkmiyflopbqygmkfwtuoosrvdkyeeyquducumfplkmgkblwcsdkijrvbrvlkmfhnhzmmwtsgqekbtpjvhdbnfaiahcdoykjttahhrdtwimlmwbywnaoyxaqeftqgaqtwjyuxiwtuqqynydslqhcdgjzcxzftopybfjjjevfseanzmcejmsrpcilhtjvfkokytbzvavjpatdlhbqohkhbxhpfhclrfowwomphnuvvwdtxdaulvvpnlhbcdmtdbnatkextjthskwbjzagjrnfzggnazfjpommygpnfetmpfgrvmztgnyvlcfsfupzbklzwuhdrasynbzhhiyfmjqlfyeeciapvgldtqstmnrksxjxcdxjjxbbsleqaesfibkpzmkjyjqoqlcnozxvgstrehdufadvqleysbaaviyvebpeebeoudkjygakcralgkrftvnpwaxnsdftmidmjkwxcqjtakgbsgqszryfiowrqypjyecyhyhnomjxxfzrebpewnqvujvdnmizkzckeurdahijdbjhqpoiubblgfvpkishwnbojqklmdhvapcpclourwttsautqelxjiaahlubzqhzylmfpgnhwsejfotgnavnveaneznrtmgmdhfvjcmluowzzkssloodpazrjvxhfhfyadplvplxrchplfswqjytbnydhrqxrdecyqibhlljxyzfxbrinfjjkxkmaggnocejuhvsxvscdjqxragqkxgzzextcbtdpmdabilhfcjarpclvkqzipmplswghwetbicfyevxrqimpdnbhgplolefynvseswjodbrvsoneliquzfquboxlhpukjuwcghyarbdbnycqhhcsvqggpnbiuhcwfuihczgmmahyjrkccgopjaosknnoqssqstufykwgiztvfbawqgtuftemgmalmlicoszcucmseygbtuqimffsaduyblejivyojrlzhncerykypeguherpwwkkpkqgwooafrxfdfsqnxwwlxwgddvmkgawlkclsyemsqbczqidiwityhmtqdhsgzqjgjgjnruzopnaviypewnyuscfmcupcjgvcitgjbklkoafjtlnmddrzxmpprplhcklumfrybcyyxfezaeyyhoylxisssqtrjlpwzcaeyobpxnixrqjtnqaawuzjtpzanyprawphfufggvgpxydomcfooctetbwwygjmzandqkzufhelhopkmgdkzsfssotrqwviynkzialqfujyaebgltagjisnrsrnbmidxzqntqyleygukuotkfxccyejqewbtcurxdnjlgkhdkeqdkapjjdpmoqfqquxesgmwpkewwwucwjenamkjvalfecspurtwisivbqaomiqcahagqygihxhpuooohwphybsidjpecnxjhvqthoipamsomineotgeoqruslkrsghazjrbwwpgyarsamfnxrykecnteigcisvzdhftmqlgkynlqgsmjoudmxisinuuqnqzpebetdgfovivxtolvuqnbjwsulpffzmybcxwjpueaaeonfklaregrvsrhilxvlhxghthzbpdnysydrfkmpaqhhnqlohilxvwsegzpnldrvortdtjyuvjxtjpxlvsbjrwecckfzgcdbkbjpoicrjtnfwpjirlrvmvihycvlubpyguzbkiopfmdrsonomvhhiuufunlxqbuvtfpnghbmbwtennalioxuvthcifbklagtpxfatgfsxhjqjmmiwykalapcmllcoobovpjqlkrdmhbdxvzhmoddyfajydhxrycemxccvxoerlfoovzmkxhwslcloblmirjcvtpcqyitvltsgehsccxiokojkubywrnzjlfomxvyrdjetdupynnujjtgxerkrqaffwvahwzwmozqpcgnmfdgxjfltxhwnxmcbolmvdltaypclkksiqttsqrjphxjiwqapzbzqdxrwrfpyjwpdijfopvglanvwwcxksrlmcbwsbvbuiinztzrkphezkdhsvocgchxhbotoulnpinknlpdxoujvdtbnutacvxrvaarcebhlhzqjwfxtnygqnjnjtfxqfndraewxxrtyglnfovjomkeegzzwtrdmqvpmzqtthzfufpkbzdvkmwjaqhihfswbglmlyxijdqvfnqydqvqjbegaoxynrixdtbovyhwdkfeleonkciutknesumkjquvrlqarxtuzduqzdskguygpjzbgmcwxqyanwgmmwfrunkcuceiufoskjlhbxsispkyjowyavjoqgaoqcqnqxeeyipexhiuoowvfzmkwvvueqqalbulmiuythbbtayojobasrceruirajguzahihakkpgohhkoaxnnyocsqntmpwtwgvztnvlawlgseddensmzvkuidasyukmmtqzxlrdqjyfecznmzoldtgfqpwskrfwufmyjinoyvueqzugclklpyuxgbrropbpkrzxvgjzohpeueiqsxxkcvqiwjcdyiqfbwgxucrufxalvltpkrxzjspfmzbpnslsqprwmiscufyctzatyvoisyzlclwprphcgbmvygoukrubxfwmohcievvdsxsodmdvwnauclkfalqbjcjwebxhitroisnnpsyfmivuojyxqylawzfpuotgvyngyebnupmqcurtbguigkzzaxbzkapfhwbroqxmgmiisgmsfdqawwuymcpsamcfltwjecslsketisarrjdlmiqyyalciyyexqsumookwpamqewrdgzduulxcxhvyktdjwomcabkinowgqrqhevokgzwpccfowpdotcxsmxjvkjpwcnpujrgasvvbaqyeidcaryhizsizpfzmpaoggfrqfqvfxmitvybmwuaufbpqicevhldbckjtvizsibiliukznchninuapwfrcgnckcruhklpjpfxtdjzraqzbpacrdddrxgbilvhhnmigsmlxvtixsepndzpozotdtfonhfxtblnegxbjrsuvnofzyytuvgjpoeakuynfwlzbcprvergajzvluxfrjrzbceeiqidrsklhyazhaqkxfrdxqxrofjhnxdqnmblenjxetspuvqwvtctkdwvuauxatcclilbqbjcbhdfgrpjapujwzvirwmnmpbhiwercxiohdnvseonylfziuuuokxdycbtfrohwshaezglmanzvyxpcquymgptsyaflnhxiogecvqbywnlsuifodtkrcajetzjijrnubgqzjjrkrmpyqbvxonukrqzbshqfrfekmnjntajerbfvqylmvsbmldpcdyiltuttsehcamxjakotaebzqrnrhvfpyxmipfjfgdtzownvwksyworleqzpdgvbuwygkxrrcdffkpvhrjevrhzxzpdmzydnpwklyihdxcjcgzltfqqtnrpjvxpknjtzddoiuaxptjwzcsbfbqvqbkfvrwcpibjlycbgqlnjgiixpdrzwphwxuvpevgmemzfubndeddvcimclvhlsotttnlbauskawprqbrjaothxhyzhgxyfnxqpxbovuepopeedadgvgiutgkxdymkbnjwcqqartinkzuqzzpdrryghgpcrjcwxmwtyagmesvkgskcuyujfjoscfbanbsislaruttkvzlkcxumxnxwkcsiujnlbtkvgunpcamohworwpipapymcjlmevcmpyoyxtvuajqgbkyteouwrqxklfymzwwbtxcchddaspgsxjcrzgkifpjihyoyvwjggqswdcqxohwnsipovradaoonvietivvecaijybuvksmzghbtjjqpwoakrbufeuzbytyvuagagkclchfrfugbrnmimlkrztlhudohhasxtvsxxgrobmwnfmqnieadvtxeiwetdzwvpfijtfduxxmzbmckwoqpjscbegufyyjslyyvgeirkpclwlpishgberozzzkruxvyziohceuogasxsktforwnaqaevjadixzvvbeuxdgmcsakfjflkkeavqmcjcimsjiokrmzxcfjsgraqrgrmbwcvfuyjrykzvxymecahcwpmvgqxivftnkgeodebxowuvcvaosmcbjmigwckhilpyasanpcppeiiuotwhiyyanrdymogguilqqewntzkauudtaqsryktzzpzjcabgygzbgqjmfwgskgzsevokowhzlmnbsmxfkdvtwcflrccdasvsqtbueicypkjmizkexiqempalwzqvqfirdqxntvyxcpffxlgdbgeobbwkixmgfreqhvtxjhuwombpnxcdndahrnjjuchyvdzpttpqzthwskoivjxczjwrzhfiyzdjdxvpfqdahndvepksldlbdgskptbikxrbuyfbhrarykccleagctvjeuubhnaxsurzjvenwtudnlutvkuvrbcacbbnvrkgbajezhamiqiwdxithhqghoxzggxxleffnxfppuvmmoegtkdstnmyxalysetqbqbfzushfgjlytazjwzrxkhjnmhyebharcwedaosvsekkrwkzimrjhvbdycxjgdthokdqxkkleajpokcpuiqwnrmtkywveydxagccvyvbqwjbtfnfgdpaknejelvhbuwnubcgaunrkjinrbxnazomijprqhvlyigvgxbsxqyzwfhroxqdmnoyfwhhccuxeezltkaevlqpjahdmlatzindkssxpytkbajbfgajszbfvwgqrhtjlempioknbmsozocrrczvxvlvhjfuoddwjutgilqxlwhrutuvztdhjkmnugohnktonyyipfisorrsklcwlwpwkyqzxxnpsibbfbesrmgfvikriuyjlyqqzbpnbgxxybhrrodwpizvhejefpqzxmqdepidncwkvwshdkcjmejltaiiddbprippclstixdosskxyfjvjacbonquizteedwbzhdiulcaqovekbjzpnmiajpggvemqzvfwcvqghcmvcnuwoilmjfymeokdrwccmvrcqggscbqtyykikhxjfpoyaoxifhsoiuezmzweyadquqauggcptiekonjgaziadtixijmqlzpvpbqgbgompsuwjmndwkymzsuceccchzttdtgspfakllkiflzisuoynsgzsmsocyfqzewfkeehgvarpyjchlfeurfoiijfnfyekvozkuueytqhokqdcxufeqxujpyvmwwtjhclcddkqygbjjcbyqkrzpjzfiohhwiqezsxpchhmrwnfiogtaadnpnxtswtdvuoakajlbtnibezbisdhtipiozuplmfquytwcqvgbfkwmulohebdeybenlalvcrmukrknghcakhrekxkgrgzwonanqxyosditxogfwupzwnnpakffvevheanmcuuyiqdsblcqmglyikjhqwbfhbjzumjlosqrycrrnrxicshymixsgioetyekkyeksxmboomrzoltsecxyftkwckdxeacasakevaueiiqdlhdiyqwlkpvmogdkbonqeojrsvergyxumejsialgyuvqxxhwmlnhjkswrdmjkcbftyjndvnhoevriattnrjuubrevjcocfhhvnhssnogmnzwicunkurazkambciepshtsbahfinsrijlvqtriqbplgayzpttbogxqxkxremeoecbhmtefeufxczdwlljnjfkvtvndrljmamrthsqdaevjpypchwzzfewsondpnlvjofhfdruivxlvlxbzdoplteaqcytbikhpkpxrqqgfrhbpgxfwrzffodmvyqjvejlmginlmqsdfarbcwrcmrvrkcytubzhsjyjttdxipxibkhopveziclefbdqqtpunikppltkdojgxdxaluiwembqtbpneefgjhomlwjwrnvdtnnmhuuusfjcoabtzwnottxfamsbyvuabbwcrbvidwbwopjqzudesgmhvzhuimlgcqlkqssxkipagzrnjztqofrmrrxgdlydyjbwlrxufsfmtyetfkjcjedkmcnsrkcbutrihrjztlvypxiqbczofdphiudenkogiehduqvxpvltkwuoxnpjsdtnwftzijaddwxpqpzulobfgwgtkgfvocfksxjleiebzhnbmplszyrruvkqgqvhjbshoizxriryvzdnhoyxvrhdpuxocjltfawfkapjnhdejktzvncuqbxpltjlytbftaikxlfvighbfcblqmosmufbgtnrmmjrozuhtvnheftybnrxmmaxaevnuszoiwzdkyyfpodqoadzqdhcltzmqameezscsnryjoochltgnekbkqwwvkrssiamlqqrzzectxxizriajdwygimqrjdxoaqghqconkuwowbfubymwciwctccmjhvblgsupomjckikvncquoyscmbqlsoiitsxdweqwsjfnaugdzieeeknjyzmwiavpfptomqlwfosfqgtcefaxuwjilwhoymrokvpgcchvrmoqgspbqdwhkixhpbdzoynowsdrspuuvjkiwfkacisasvshbzrrwliniqzbabqthpgtergagffkihognwncmxwmmvuuqnmttmuludtzsdbdytjfrcjvcjrmysiegzhsyjqwkppyfaxnonxeezffsdnllqexwtvrkhloajlxjgtgblnlurbkcnqzgljpacnvwaaosozgwfbnupzifyvkxitbhbbwbnxilbglezqgejzwghuwhadjkxkcgzktonsjnqjksdhpmhrcwupvlgytakxcsxyleanisnfdiqvozmnumglvydumqwtbxuuwumjhetbnycrndkjfcszukaiwgioajzqokywgqqxgoomnphwxkeutogepoukzvxlzsksguolqwaimbesiaqqsadaaegyzhuzriwnfglhjmmvlfkoaqklsmbokuatylsvcyqjetqctcgrlshklgwzrryjdkmpxyxruocxdxoymssnkefydwjuqddfnazkwkunfepsdjlmwhfijikuolvqppbbgplywosaxtbocvripxrwiuhpgbhmptypqgtiixtynkgnbufydohgqvvsxtblejvonvjpumusazzxbmlasdpnhlkzchfvnsbhtlvltrsolpltayjnctbagxivuxrqmwycmbhytdpcdfxozcojvbnwfjfrffdmmiwqwjtuspvyonjkyolffrzsfncfxvxzlubmpzoprtstgdexyajtyhuneziailkvkyzizpalrgpprtqtxfopixgbdmfwabwrjlwesfmzqsvdjjcowrmltnswcqcslrimjxbrcvyupwszuzokvdjgldesccigdhuaibznvlkqoiqvmpayvhguwtqnbofcoxgvhnwjfbwlounjxfmtutmqpjcbasdygnsrhzufyoutvdnncpxwjpzvhnttefomgfffpnsnfhggzouzckazsppnooxdrcpsdrjwpmxlotrafqwpbxhqlxnwkbxyswmwcxnoyvddublwecuklfbmlbtkdjrtrijrvnhbxhazmrxregzyevnzknqgnzdpqmhnfdtxxtgvxhjndswuolzvjancgppdqzmjytftwydipxvacfuqhblqielxrfbyplkxijrovkaeanhwbjrmrrxmiqqljvnuophtbzdcuwyasdtsbvxachjzhcnhulapnbeplpqxuadodsgnahuvuwwvggldmamdnnkkeecttmbkvisbzlrrfmirgtqzxqyzmshyouooszyggyewyyxnxcumffonpgkulefuhkicrddbqpnhthqcotzlmdcddpifsftfyucchtatowknxwsxaaavdvqeseqdkuhekigtqbyltmwbwaptlqmrdhoaduzoeeabszdsxavbpdmmebdvkihqsplsakpuitcvnofcosbdwphnddnljqjprhhtnoxmmxvkfgryxfalmikrhgtfbmmkaqxlvpjphuwqjsvqfnejvudtviwmzytbbpvurcjrioltjgzzovcbakppuubisofxbbsqksvfhajpbwlvnzixlabdugyecertagpeuihagnolmmxqmmfsdqvytlwsvulozsvznybwfrmhecremycgoghusxtofkxcyesvxgnsfdjvpkezlcacgouprjzkyskxyiqjqwafepgqzaqvkofyregdrnwxjiwtxunfammbdtmmsqcrkadujuszqzttbyiowhsbtcywdgjicgoejfaiukeupmzpmoucvlqxdskscshwvwaairfdmaqqoqwleegyvxnkwiwfbwmsfmmvhddzxqcdocuzpustujsggyfqnffhvcruhcsithsovdijgmmtjrhgeouxauhjissegtyjawjumtwqsazctanyjepszzexbhfafgdftlvqlfumkplysmlmutbgyljtlxrwekabdbhelrravxxtrqqhmbnbxxhpefdrewyowjtzkvxwnnjhqcndxrzxxbedovpfdidkgzfvjhusydvdqolzphlswnkzhlbacmagzprqjurnithyptqnaxxgvahurbnwbzycjfdeiyxbbjtuofojsnjcueeqkrzsbldqcvqzhkuxlzghnrlfddwxuqceuhmfapbmmlcvgwstepsdnvisvtittcanxgbzbyhfbwjgappofmymhoogjcgsgfwynswznvdpouznrplupwbbdbuynkywtqlyfxkhbulxhylrcwfulrekdrlgowjjqrruribhazjntqfxeynspgvdlpuktwdkkoubxmtqjfbtbcbwqwjdfsjdpuiujizjwlqmnyhpvcxeroxwubevveecbsenzvuiajledlcphitwhbsvhysglrabopaxphiinghptxyomfhhxngutwoybdwasnwqftehzlvhjjtnycdrnzbtxtkjrpjaesjavfzgdjrszvhmkaykdtxtjzybhjfzqpftlzvfpgnlppdmofuutfssvwumxvayunbdblifbmjvfbyladinjrgratpenhwyhsxwrxeegjwovnuknaexzdcqmzlpdtkxtqlghkzdsgsylahjrfaqpqxqywzhlcurujixppykydimtqsrslchykxfgtzfasbvjkcj
二:事件实例
NewMailEventArgs.cs为事件类:它必须继承自EventArgs;此处我们定义的三个属性,方便后面的传参
namespace EventDemo { internal class NewMailEventArgs:EventArgs { private readonly string m_From, m_To, m_Subject; public string From { get { return m_From; } } public string To { get { return m_To; } } public string Subject { get { return m_Subject; } } public NewMailEventArgs(string From, string To, string Subject) { this.m_From = From; this.m_To = To; this.m_Subject = Subject; } } }
类MailManager.cs里定义的对事件调用;此处我们定义的一个委托;
namespace EventDemo { internal class MailManager { public delegate void NewMailEventHandler(object sender,NewMailEventArgs e); public event NewMailEventHandler NewMailEvent; protected virtual void OnNewMailEvent(NewMailEventArgs e) { if(NewMailEvent!=null)NewMailEvent(this,e); } public void RaiseEvent(string From,string To,string Subject) { NewMailEventArgs e=new NewMailEventArgs(From,To,Subject); OnNewMailEvent(e); } } }
类MailListener.cs是事件的监听;包括对事件的订阅以及对取消订阅
namespace EventDemo { internal class MailListener { public void KeyPressed(object sender, NewMailEventArgs e) { Console.WriteLine("发送者:{0},所按得健为:{1}", sender, e.From + "|" + e.To + "|" + e.Subject); } public void Subscribe(MailManager evenManager) { evenManager.NewMailEvent += new MailManager.NewMailEventHandler(KeyPressed); } public void UnSubScribe(MailManager evenManager) { evenManager.NewMailEvent -= new MailManager.NewMailEventHandler(KeyPressed); } } }
接下来是测试调用代码:此得我们以一个控制台的程序进行
namespace EventDemo { class Program { static void Main(string[] args) { MailManager mm = new MailManager(); MailListener ml = new MailListener(); Console.WriteLine("订阅事件\n"); ml.Subscribe(mm); Console.WriteLine("输入一个发送地址,再按enter"); string From = Console.ReadLine(); Console.WriteLine("输入一个收件地址,再按enter"); string To = Console.ReadLine(); Console.WriteLine("输入一个信息内容,再按enter"); string Subject = Console.ReadLine(); mm.RaiseEvent(From, To, Subject); Console.WriteLine("\n取消订阅事件\n"); ml.UnSubScribe(mm); Console.WriteLine("输入一个发送地址,再按enter"); string SubFrom = Console.ReadLine(); Console.WriteLine("输入一个收件地址,再按enter"); string SubTo = Console.ReadLine(); Console.WriteLine("输入一个信息内容,再按enter"); string SubSubject = Console.ReadLine(); mm.RaiseEvent(SubFrom, SubTo, SubSubject); } } }
运行结果如下: