My intent is to reuse the SelectedValueChanged
event inherited from the ComboBox
Class (which, in turn, inherits it from the ListControl
Class)
In the code below: SelectedValueChanged
is tagged with the compiler error shown in the screen shot. I do not intend on hiding
the inherited event, so I do not want to use the new
keyword. I want the classes that I derive from DRT_ComboBox_Abstract to be able to use the inherited event as-is.
How do I define an EventHandler
using an event inherited from a base class? (Or, am I totally off the planet with regard to understanding events?)
Note: "Show Potential Fixes" surrounds public event EventHandler SelectedValueChanged
with #pragma warning disable CS0108
which just disables the warning.
using System;
using System.Windows.Forms;
namespace DRT
{
internal abstract partial class DRT_ComboBox_Abstract : ComboBox
{
//SelectedValueChanged is tagged with the compiler error shown in the screenshot
public event EventHandler SelectedValueChanged;
public DRT_ComboBox_Abstract()
{
InitializeComponent();
}
public void Disable()
{
this.Enabled = false;
}
public void _OnSelectedValueChanged(object sender, System.EventArgs e)
{
this.SelectedValueChanged?.Invoke(sender, e);
}
}
}
You don't need to declare the event again. If it is public and it is being already thrown when it needs, you can handle the changes if you need, by subscribing to the base class event.
I mean, you can do something like:
using System;
using System.Windows.Forms;
namespace DRT
{
internal abstract partial class DRT_ComboBox_Abstract : ComboBox
{
public DRT_ComboBox_Abstract()
{
InitializeComponent();
SelectedValueChanged += MyOwnHandler
}
protected virtual void MyOwnHandler(object sender, EventArgs args)
{
// Hmn.. now I know that the selection has changed and can so somethig from here
// This method is acting like a simple client
}
}
}
On S O LID classes (I believe it is the case for ComboBox
), often the method that effectively invokes the subscribers to handle some event is usually virtual, allowing you, once you're inheriting from this class, intercept the event handlers invocation, if it is what you want.
It is:
using System;
using System.Windows.Forms;
namespace DRT
{
internal abstract partial class DRT_ComboBox_Abstract : ComboBox
{
public DRT_ComboBox_Abstract()
{
InitializeComponent();
}
protected override void OnSelectedValueChanged(object sender, EventArgs args)
{
// Wait, the base class is attempting to notify the subscribers that Selected Value has Changed! Let me do something before that
// This method is intercepting the event notification
// Do stuff
// Continue throwing the notification
base.OnSelectedValueChanged(sender, args);
}
}
}
Note: I've removed the Disable
method just for code simplification. It's out of scope in this subject
I hope it helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.