简体   繁体   中英

C# Custom Control Properties

I am building some applications that use OPC to control some industrial automation. I have decided it might be a good idea to create some custom controls for standard things I will use such as buttons and text boxes. One of my main reasons was that I felt in an ideal pattern the end object ie the button is the one who holds information about the OPC item it refers to otherwise in a typical application my Form class gets polluted by tons of variables that are in my mind scoped more global then they should. I started then by using the Tag property of the button but this requires some overhead code that is the same for each instance. I felt like the right thing to do is subclass controls that I would like to use and provide properties to configure each one.

With that context in mind my real question is this. To make it as portable as possible I decided there should a property to define the OPC Group. I declared a property like this

public class OPCButton : Button  
{  
    [Category("OPC")]
    public OPCConnectedGroup
    {
        get { return _OPCGroup; }
        set { _OPCGroup = value; }
    }
}

This shows up int the property list when I add the control but I am unable to bind this property even though my Form1 contains

public OPCConnectedGroup Connection1 = new OPCConnectedGroup();

I have resolved that the way to solve this is to probably create an additional control like OPCGrp that can be added to a Form then the Controls can reference this. To test I added a property of type Button and sure enough when I added it and browsed to the property it gave me options for all the buttons on the Form. I have no huge problems with this approach I just want to make sure that Im following a prototypical pattern because I will be responsible for maintaining the control library but not always implementing and Im trying to get it down to a 1-2 step process to implement a control.

Thanks Matt

When defining OPCConnectedGroup you can inherit Component class.

This way if you have a property of type OPCConnectedGroup in your OPCButton , then at design time, you can put instances of OPCConnectedGroup on the form, and then if you choose your OPCButton at designer, that property of type OPCConnectedGroup will show as a drop down list that you can select one of instances that you put on the form for it.

Example:

If I have such MyButton and MyClass :

public class MyButton : Button
{
    public MyClass MyClassInstance { get; set; } 
}

public class MyClass : Component
{
   public string SomeProperty {get;set;}
}

Then you can put some (or one) instance of MyClass on the component tray of the form:

在此输入图像描述

And then if you select MyButton on your form, you can choose one of MyClass instances from in property grid:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM