question

Shervan360-6172 avatar image
1 Vote"
Shervan360-6172 asked JackJJun-MSFT commented

Add and Delete Event in C#

Hello,

I created simple Windows Forms app in C#.
Why is it not possible to remove from the clickMeButton1.Click event?
I expected ClickMeButton1_Click method executed one time because I detach method from event with clickMeButton1.Click -= ClickMeButton1_Click;

The whole project in OneDrive:
https://1drv.ms/u/s!AiKKqJaRZheDognJPo0Ih379wgBQ?e=9qDZVY

224994-screenshot-2022-07-26-160752.png

 namespace WinFormsApp1
 {
     public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
         }
    
         private void Form1_Load(object sender, EventArgs e)
         {
             clickMeButton1.Click += ClickMeButton1_Click; //execute 
             clickMeButton1.Click += ClickMeButton1_Click; // execute
             clickMeButton1.Click += ClickMeButton1_Click; // execute
         }
    
         private void ClickMeButton1_Click(object? sender, EventArgs e)
         {
             MessageBox.Show("Hi!"); //executed 3 times
             clickMeButton1.Click -= ClickMeButton1_Click;
             clickMeButton1.Click -= ClickMeButton1_Click;
             clickMeButton1.Click -= ClickMeButton1_Click;
             clickMeButton1.Click -= ClickMeButton1_Click;
             clickMeButton1.Click -= ClickMeButton1_Click;
             clickMeButton1.Click -= ClickMeButton1_Click;
             clickMeButton1.Click -= ClickMeButton1_Click;
             clickMeButton1.Click -= ClickMeButton1_Click;
             clickMeButton1.Click -= ClickMeButton1_Click;
             clickMeButton1.Click -= ClickMeButton1_Click;
             MessageBox.Show("Bye!"); //executed 3 times
         }
     }
 }






dotnet-csharpwindows-forms
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Shervan360-6172, Welcome to Microsoft Q&A, If you want ClickMeButton1_Click method executed one time, there is no need to add and delete events many times.
Based on my test, Please try the following code to get you wanted:

  private void Form1_Load(object sender, EventArgs e)
         {
             clickMeButton1.Click += ClickMeButton1_Click;
         }
    
         private void ClickMeButton1_Click(object? sender, EventArgs e)
         {
             MessageBox.Show("Hi!");
             clickMeButton1.Click -= ClickMeButton1_Click;
             MessageBox.Show("Bye!");
         }

When you click the button at the second time, it will not show any messagebox.

0 Votes 0 ·

1 Answer

reza-aghaei avatar image
0 Votes"
reza-aghaei answered reza-aghaei edited

It's expected behavior.

You have assigned the event handler 3 times, right? Then when you click on the button it will run 3 times, no matter what you do inside the event handler.

So it basically does this for 3 times:

  • Show Hi

  • Remove event handler 10 times

  • Show Bye

After completing these 3 rounds, now if you click on the button again, it doesn't run the event handler, because you have removed the event handlers. Well, as a side-note, you don't need to remove the event handler 10 times; 1 time will be enough.

You should not assign the event handler more than once. If you assigned, then you cannot disable it inside the event handler itself, it's too late.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.