简体   繁体   中英

Create User Control from existing controls

I have a project with one Windows Form, which contains large numbers of controls. As I read in Divide large windows form in pieces , I can divide it in few User Controls.

How to create a User Control from few existing controls (when they have the code assigned to its behaviour - ButtonClick etc)? Is there a automated way to do it in Visual Studio or I have to manually copy the controls in Designer view and then cut and paste the code behind the controls?

Thanks!

This will not only have to be done manually .

The by far larger but also more beneficial part will be to create useful interface methods and properties for those UCs.

All their controls will become private members and won't be able to work with those in other UCs. So you will have to pick all those dependencies apart and create well defined Properties in the UCs that take the place of all interactions between the controls that go onto different UCs.

This will be a lot of work but, if you follow it through it will be well worth it.

Do not fall for just making them public and/or create references between all UCs just to make it easier. Instead create meaningful public methods, properties and delegates to express what is meant to happen and not what was coded before whaen everything was just slapped onto the same Form!

The more you can hide between the UCs the better the design will get..

Example: There may at the moment be a CheckBox that lets the user switch between two modes.

Make that mode a Property that has a nice name and is cotrolled by the CheckBox . Do not make the CheckBox public! Most often one UC should never know about any Control of any other UC but only of a few Properties.

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