Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create a C# property that can subscribe to another WinForm control at design time?

I want to create a property in a control that will act as a viewer that will be able to connect to another non-visual control to show its current status. In this example let’s say that the viewer will simply show the status of online or offline.

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

Selecting an instance (Heater1) in the Source property would subscribe HeaterMonitor1 to all the status updates generated by Heater1.

Is there an existing pattern I can follow as a template?

If it makes a difference I can use .net 3.5 and higher. I selected data-binding as a tag, but I'm not sure that is correct because this is not a database question. But it does seem similar to a DataGridView selecting a DataSource property.

Edit #1: Based on the comments so far I don't think I emphasized enough what I'm trying to get. I want the property editor to list the eligible IHeater controls on the form. I don't have an issue with creating a regular IHeater property that I can assign at run-time.

like image 833
Rich Shealer Avatar asked Nov 30 '15 15:11

Rich Shealer


1 Answers

To have non-UI elements that can be used at design-time 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 design-mode (in component tray):

enter image description here

And select the source this way:

enter image description here

like image 118
Reza Aghaei Avatar answered Sep 23 '22 20:09

Reza Aghaei