简体   繁体   中英

How to make a UserControl inert at design time?

I have a UserControl that does some cool video capture stuff. Now I slapped that control on a form - and it starts working right away, even in design time! That's very gratifying and all to see it working, but it's kind of annoying at design time, and it slows me down. I know that the UserControl is effectively in run-time when it's being used on another form/control at design time, but I'd like to be able to shut it off when it's not in an actual running app.

What's the best way to do this?

Put something like this code in your control:

if(DesignMode)
   return;

DesignMode is the property that tells you you're running in the designer.

UserControl has a property called DesignMode - you can just jump out of any of the control logic if this is true.

Alternatively, if the design approach of your project fits it, you could implement binding on it, port all the working logic out to a non-UI controller class, and only set the controller DataSource in the constructor. The designer usually realises it can't anticipate what the instance assigned to it is going to be, so doesn't try.

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