简体   繁体   English

扩展用户控制并保留设计模式

[英]Extend Usercontrol and preserve design mode

I need to add some functions and properties to a user control for a specific project, and to avoid writing them once and again and again I would like to write my own user control extending the existing one. 我需要为特定项目的用户控件添加一些功能和属性,并避免一遍又一遍地编写它们,我想编写自己的用户控件来扩展现有控件。

Of course I need also the design mode unaltered in my new version. 当然,我还需要在新版本中保持不变的设计模式。 What is the best way to do this? 做这个的最好方式是什么?

My project is a Windows forms project in visual studio 2015 我的项目是Visual Studio 2015中的Windows窗体项目

You can create a custom class which inherits from the other control, and you can add methods, properties and also override existing ones. 您可以创建一个自另一个控件继承的自定义类,还可以添加方法,属性并覆盖现有的方法,属性。 The design would remain the same: 设计将保持不变:

public class MyControl2 : ExistingControl {

    public MyControl2() {
        // Initialize
    }

    public void Method1() {
        // Code
    }

    public int SomeProp
    {
        get { return 3; }
        set { /* Do stuff (with value) */ }
    }

    // Overriding existing functions
    public override int SomeValue()
    {
        return 45;
    }

}

And then call it: 然后调用它:

MyControl2 x = new MyControl2();

Or (If you don't want to use code to manually load the component) you can simply build and add it to the interface by drag and dropping it from the toolbox. 或者(如果您不想使用代码手动加载组件),只需将其从工具箱中拖放,即可构建并将其添加到界面中。

I hope I understood what you meant to do : ) 我希望我能理解你的意思:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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