简体   繁体   中英

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

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 : )

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