Search code examples
c#event-handling

cannot add or remove an event handler "method group" but the designer can


When I try to add or remove an event handler the error is "cannot assign … to a "method group". Yet the exact line of code in the designer compiles fine.

I am filling in list boxes so the user can select the items. There are several SelectedIndexChange fire backs that I do not want to fire until I have everything in place. I no no trouble coded a numeric up-down to prevent it firing but I cant get the listbox coded and am forced to use semaphores to prevent unwanted things from happening. I looked at the VS2017 build "xxx.Designer.cs" and copied and pasted the exact line of code into the "xx.cs" but that error shows up.

        {
            cb_AppNames_SelectedIndexChanged -= new System.EventHandler(this.cb_AppNames_SelectedIndexChanged);
            FillAppBox();
            cb_AppNames_SelectedIndexChanged += new System.EventHandler(this.cb_AppNames_SelectedIndexChanged);

here is the code from the Designer.cs

            // cb_SelProj
            // 
            this.cb_SelProj.FormattingEnabled = true;
            this.cb_SelProj.Location = new System.Drawing.Point(86, 25);
            this.cb_SelProj.Name = "cb_SelProj";
            this.cb_SelProj.Size = new System.Drawing.Size(279, 21);
            this.cb_SelProj.TabIndex = 0;
            this.cb_SelProj.SelectedIndexChanged += new System.EventHandler(this.cb_SelProj_SelectedIndexChanged);```

fixme1.png shows error messaged and fixme2 shows code that has no err
![1](http://stateson.net/images/fixme1.png)
![2](http://stateson.net/images/fixme1.png)



Solution

  • You are basically correct but the actual problem was (1) about 3 in morning looking at this, (2) was unable to get .png file to show up at this forum - still do not know what I did wrong, and (3) due to not being able to see the .png in the "big screen" so I did not notice I was using "_" instead of "." when I tried to code the following up

    this.cb_AppNames.SelectedIndexChanged -= new System.EventHandler(this.cb_AppNames_SelectedIndexChanged);
    

    I was using small font in VS2017 for code and did not see the problem though it is plain here:

    this.cb_AppNames.SelectedIndexChanged -= new System.EventHandler(this.cb_AppNames_SelectedIndexChanged);
    this.cb_AppNames_SelectedIndexChanged -= new System.EventHandler(this.cb_AppNames_SelectedIndexChanged);