c# - The Button in WinForms has a CLICK event. But who tells the Button object that it has been clicked? -


i learning c#. going through events , delegates part of language. working on winforms application educating myself. tried looking deep understanding buttons , how work. found following:

1) there line public partial class form1 : form in default form1.cs file. partial class.

2) have form1.designer.cs class file has line partial class form1. files mentioned in 1) , 2) combine form full class.

3) from1.designer.cs file has lot of statements create button object. has statement of particular interest me:

this.btn_basebuildlocation.click += new system.eventhandler(this.btn_basebuildlocation_click); 

this statement adds custom function delegate click. delegate declared in control class (system.windows.forms.dll) follows:

public event eventhandler click; 

4) eventhandler delegate defined in system.eventhandler.cs (mscorlib.dll).

5) button class inherits control class , has access click eventhandler.

6) button class has logic handle flow once knows has clicked it. had @ button class used in mono understanding inner details. classes want learn.

7) extremely beautiful. troubled fact did not know how button object knows has been clicked.

8) went through vc++ , how handles events. found lot message loops, event queues etc...

questions:

1) vc++ way of handling events same .net's?

2) if so, there way details?

any appreciated. thanks.

a winforms button managed wrapper around unmanaged windows type created , managed via set of win32 api calls .net performs p/invoke on.

deep down, button subscribes same window event loop (or message pump if prefer) drives win32 api calls may have seen in vc++ examples. unmanaged windows runtime puts events (like "this button has been clicked") onto event queue. when loop executes, queued event picked relevant control , propagated "managed" event when able observe it.

in essence, windows runtime providing of infrastructure , .net provides convenient set of wrappers make easy work clunky old win32 libraries.

you can discover lot of if use reflector , dig button , control see .net code ends , unmanaged win32 calls begin.


Comments