Question Remove Event Handler

arun

Member
Joined
Dec 15, 2021
Messages
8
Programming Experience
5-10
hi
I want to remove the Combobox Event Handler "SelectedIndexChanged"
combobox.SelectedIndexChanged -= new System.EventHandler(this.cboDrugTypeEdit_SelectedIndexChanged);

I am getting a green scoogly underline this.cboDrugTypeEdit_SelectedIndexChanged
Nullability of reference types in type of parameter 'sender' of void selectedIndexChanged(object sender, eventargs e) doesnt match the target delegate EventHandler

What this Warning mean ? However the program works

Thank u
 
Try changing this:
C#:
combobox.SelectedIndexChanged -= new System.EventHandler(this.cboDrugTypeEdit_SelectedIndexChanged);
to this:
C#:
combobox.SelectedIndexChanged -= cboDrugTypeEdit_SelectedIndexChanged;
and see whether that makes a difference.

By the way, do you really have a ComboBox control named combobox? Not cool. Why is it using a method to handle its event that is seemingly intended for a ComboBox named cboDrugTypeEdit?
 
Try changing this:
C#:
combobox.SelectedIndexChanged -= new System.EventHandler(this.cboDrugTypeEdit_SelectedIndexChanged);
to this:
C#:
combobox.SelectedIndexChanged -= cboDrugTypeEdit_SelectedIndexChanged;
and see whether that makes a difference.

By the way, do you really have a ComboBox control named combobox? Not cool. Why is it using a method to handle its event that is seemingly intended for a ComboBox named cboDrugTypeEdit?
Eh sorry it should have been ComboBox typo
 
That's even worse...
 
Nullability of reference types in type of parameter 'sender' of void selectedIndexChanged(object sender, eventargs e) doesnt match the target delegate EventHandler
C# 8.0 and higher versions and compiler enables stricter null checking. .NET 6 uses C# 10 as I recall. The green squiggly lines are just warnings, but in general you should heed them.

If you notice the new EventHandler is declared as:
C#:
public delegate void EventHandler(object? sender, EventArgs e);
where as the older versions of the would have:
C#:
public delegate void EventHandler(object sender, EventArgs e);

Note the nullable mark ? after the object.

Your event handler was likely declared as:
C#:
void cboDrugTypeEdit_SelectedIndexChanged(object sender, EventArgs e)
and so the compiler was highlighting that it was expecting to see object? for the delegate being passed to it, but yours only has object.
 
Back
Top Bottom