0

I have a UserControl that implements an interface class IPropertyListItemType. I would like that the interface class requires it to override the Enabled property of the control. Within my interface class I have:

bool Enabled { get; set; }

Within my derived class (i.e. the UserControl), I currently have:

        bool IPropertyListItemType.Enabled
        {
            get { return _enabled; }
            set 
            {
                _enabled = value;
                lblStatus.Text = _enabled ? "Enabled" : "Disabled";
                //lblStatus.BackColor = _enabled ? SystemColors.Window : SystemColors.Control;
            }
        }

However, when the Enabled property on the user control is set, the code within the Enabled property of the UserControl is not executed, just that of the UserControl. I cannot place override in the interface class (The modifier 'override' is not valid for this item), or on the derived class (again, The modifier 'override' is not valid for this item).

How can I require that an override is implemented on the derived class?

Mark Roworth
  • 409
  • 2
  • 15
  • [Enabled](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Control.cs,2428) is not virtual, you can't *override* it. [Explicit](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/explicit-interface-implementation) interface implementation needs to be called specifically by casting to interface first. – Sinatr Jun 22 '21 at 10:09
  • *"when the Enabled property on the user control is set"* - what exactly you do? Also, are you talking about run-time or design-time, perhaps even both? – Sinatr Jun 22 '21 at 10:13
  • Do you mean to change it in `public new bool Enabled { ... }` (shadow it) and get/set `IPropertyListItemType.Enabled` instead? Or also get/set the `base` property value? Which may get *weird*. What do you actually want to do? -- It's not just because of the name of a property in that Interface, right? – Jimi Jun 22 '21 at 10:14
  • @Jimi - think I need to read up on the difference between shadows and overrides. – Mark Roworth Jun 22 '21 at 10:43
  • @Sinatr - runtime. I create an instance of the derived class (which inherits UserControl and implements IPropertyListItemType) and set the Enabled property on it. – Mark Roworth Jun 22 '21 at 10:43
  • Thinking that I shouldn't use Enabled, even thought I'm enabling/disabling the UserControl and perhaps use a similar name that doesn't class between the two interfaces. Seems it would add clarity in the calling code as to what is actually getting run. – Mark Roworth Jun 22 '21 at 10:46
  • @Jimi - I want to handle some of the graphical changes within the UserControl when it is disabled manually. I'm going to introduce an AppearEnabled property as well, so the names don't clash. i.e. I don't really, really need to, but it would haven't been neater. – Mark Roworth Jun 22 '21 at 11:09
  • Well, since you cannot **override** the Property, you can **shadow** it, if you want (already posted that). Really really want, since, as you of course know, the `Enabled` property is kind of *special*. – Jimi Jun 22 '21 at 11:23

0 Answers0