I have two System.ComponentModel
classes, ComponentA
and ComponentB
:
public class ComponentA : Component
{
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ComponentB ComponentB { get; } = new ComponentB();
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Timer TimerA { get; } = new Timer();
}
public class ComponentB : Component
{
//[Browsable(false)]
public event EventHandler ComponentBEvent;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Timer TimerB { get; } = new Timer();
}
Notice how class ComponentA has a property of type ComponentB and that class ComponentB has a property of type Timer (a System.Windows.Forms.Timer). ComponentA also has a property of type Timer although that is not relevant yet. ComponentB also has an event (ComponentBEvent), this fact being key to what I discovered.
As is, those classes will produce what I am looking for, which is the ability to assign event handlers to events in nested components by using the Properties Window. The following screenshot of the Events tab of the Properties window shows how it is possible to assign an event handler to the Tick event of the TimerB property in ComponentB.
The source code for ComponentB above has the Browsable attribute for the event ComponentBEvent commented out. Uncommenting the attribute as in:
public class ComponentB : Component
{
[Browsable(false)]
public event EventHandler ComponentBEvent;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Timer TimerB { get; } = new Timer();
}
has the unfortunate effect that ComponentB completely disappears from the Events tab of the Properties window making it impossible to assign an event handler to the TimerB property of ComponentB.
So to summarize, what I found is that to be able to use the Properties tab in the Properties Window to assign an event handler to a component that has a 3rd level of nesting (TimerB) it is required for the containing component (ComponentB) to have an event (ComponentBEvent in the example). This is only a problem for components with a 3rd level of nesting (or greater I presume). The class ComponentA does not have any events but this does not preclude one from assigning event handlers to its TimerA property as the first screenshot shows.
The issue I encountered only affects the Events tab of the Properties Window. The Properties tab displays properties for all components as expected.
My question is, how can I use the Properties tab of the Properties Window to assign event handlers to a component with a 3rd level of nesting and not be forced to having an event in its containing component?
My tests were done with a Windows Forms application in a .NET Framework 4.7.2 project.