Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

.net - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...