Adding and Removing Events.

Yeah. Small thing.. but I didn’t notice about that.. I think I might never notice about that if my friend “Lu taw lay” didn’t tell me…

Actually, when I was working on automation team, we needed to write the automation script for ComponentOne Control to ensure whether each property/ method and event of a particular control are working fine or not. When we were writing the scripts, we needed to add/remove the event at runtime. In case of removing a event from the control, we wrote like this.button1.Click -= null wrongly in our script. and we didn’t notice about that til the time my friend tell me.

VB
If CheckBox1.Checked Then
AddHandler Button1.Click, AddressOf button1_Click
Else
RemoveHandler Button1.Click, AddressOf button1_Click
End If

C#
if(this.checkBox1.Checked)
this.button1.Click += new System.EventHandler(this.button1_Click);
else
this.button1.Click -= new System.EventHandler(this.button1_Click);

References..
Handling Events in .NET

Posted in C#

Leave a Reply

Your email address will not be published. Required fields are marked *