How do you create a C # property that can subscribe to another WinForm control during development?

I want to create a property in a control that will act as a viewer that can connect to another non-visual control to show its current status. In this example, we can say that the viewer simply shows the status online or offline.

I want to be able to remove the non-visual control, let's call it Heater type IHeater in the form and then release HeaterMonitor . I want to go to the HeaterMonitor properties and for the custom Source property, see a list of all the IHeaters currently in the form.

Selecting an instance ( Heater1 ) in the Source property will subscribe HeaterMonitor1 to all state updates created using Heater1 .

Is there an existing template that I can use as a template?

If that matters, I can use .net 3.5 and above. I chose data-binding as a tag, but I'm not sure if this is correct because it is not a database issue. But it looks like the DataGridView is picking the DataSource property.

Edit # 1: Based on comments so far, I donโ€™t think I have stressed enough what I am trying to get. I want the property editor to display the appropriate IHeater on the form. I have no problem creating a regular IHeater property that I can assign at runtime.

+5
source share
1 answer

To have elements other than the UI that you can use during development in the designer, you can inherit from Component .

 using System.ComponentModel; public interface IHeater { int Temperature { get; set; } } public class Heater : Component, IHeater { public int Temperature { get; set; } } public class HeaterMonitor:Component { public IHeater Source { get; set; } } 

Then you can use them in development mode (in the component tray):

enter image description here

And select the source this way:

enter image description here

+5
source

Source: https://habr.com/ru/post/1237111/


All Articles