This is an example of a custom event. C #, WinForm, Visual Studio 2017.
Draw a meadow in hovertreeform with a lot of grass (analogue).
Hewenqitianyi simulates the weather, triggering a "wind" event (hovertreewindevent) with wind or westerly winds, or stationary.
When the wind blows, the grass goes west, and the west winds to the east. Still the grass is not Huidong crooked West.
Every grass on the grass listens to the Hovertreewindevent event and adjusts its stance according to the wind direction (windddirection).
There are timers in the Hewenqitianyi that trigger an event that adjusts the wind direction every once in a while.
As the "wind" incident heard by the supervisor is not a thread in WinForm, to change the posture of "grass" in WinForm,
The BeginInvoke method and delegate are used to access the control outside of the WinForm thread. See the Hovertreegrass user control specifically.
by why Ask
Reference: http://hovertree.com/h/bjag/k0qps1iw.htm
Http://hovertree.com/h/bjag/cm8k4ja1.htm
:
Hewenqitianyi Class Code:
usingSystem;usingsystem.timers;namespacetianyihewenqi{classHewenqitianyi { Public Static EventActioneventhandler hovertreewindevent; Windeventargs _arg=NewWindeventargs (); PublicHewenqitianyi () {Timer H_timer=NewTimer (); H_timer. Interval= the; H_timer. Elapsed+=h_timer_elapsed; H_timer. Start (); } Random _hoverclock=NewRandom (); Private voidH_timer_elapsed (Objectsender, Elapsedeventargs e) {_arg. Windtype= (windddirection) (_hoverclock.next (3)); OnAction (_ARG); } protected voidOnAction (Windeventargs ev) {hovertreewindevent?. Invoke (EV); //equivalent to the following code//if (hovertreewindevent! = null)//{ //hovertreewindevent (EV); //} } } classWindeventargs:eventargs { PublicWindddirection Windtype {Get;Set; } } enumwindddirection {East, West, Static}Delegate voidActioneventhandler (Windeventargs ev);}
Custom User Control Code:
usingSystem;usingSystem.Windows.Forms;namespacetianyihewenqi{ Public Partial classHovertreegrass:usercontrol {Delegate voidMysettext (stringtext); PublicHovertreegrass () {InitializeComponent (); Hewenqitianyi.hovertreewindevent+=hewenqitianyi_hovertreewindevent; } Private voidUpdatelabel (windddirection wd) {if(Label_grass. invokerequired) {//when a control's InvokeRequired property value is true, it is stated that a thread other than the one that created it wants to access itAction<windddirection> actiondelegate = (x) = = { Switch(x) { CaseWindDdirection.East:label_grass. Location=NewSystem.Drawing.Point ( +-9, Label_grass. LOCATION.Y); Label_grass. Text="\\"; Break; CaseWindDdirection.West:label_grass. Location=NewSystem.Drawing.Point ( ++9, Label_grass. LOCATION.Y); Label_grass. Text="/"; Break; CaseWindDdirection.Static:label_grass. Location=NewSystem.Drawing.Point ( +, Label_grass. LOCATION.Y); Label_grass. Text="|"; Break; } }; //or//action<string> actiondelegate = delegate (string txt) {This.label_grass. Text = txt; }; This. Label_grass. BeginInvoke (Actiondelegate, WD); } Else { Switch(wd) { CaseWindDdirection.East:label_grass. Text="\\"; Break; CaseWindDdirection.West:label_grass. Text="/"; Break; CaseWindDdirection.Static:label_grass. Text="|"; Break; } } } Private voidhewenqitianyi_hovertreewindevent (Windeventargs ev) {Updatelabel (EV). Windtype); } }}
Hovertreeform Form code:
usingSystem.Windows.Forms;namespacetianyihewenqi{ Public Partial classHovertreeform:form { PublicHovertreeform () {InitializeComponent (); for(inti =0; I < Tablelayoutpanel_hovertree. ColumnCount; i++) { for(intj =0; J < Tablelayoutpanel_hovertree. RowCount; J + +) {Tablelayoutpanel_hovertree. Controls.Add (NewHovertreegrass (), I, j); }} Hewenqitianyi h_ty=NewHewenqitianyi (); } }}
Tianyihewenqi Project Source code download
C # Custom events simulate wind-blown grass rocking