Search code examples
c#winformsuser-controlssystem.reflection

Winforms disable data preview


I'm trying to generate a list for my combobox. This is done when the usercontrol is loaded. But because I'm generating this list based on which classes implement a certain interface, the preview of the form crashes. So this usercontrol is inside of the form and the form tries to "load" the usercontrol, cannot find the data and throws an error. I want to disable the preview of data and just show the empty usercontrol

How do I do this?

Screenshot of the error:

Screenshot of the error

Piece of code that makes the form crash:

var type = typeof(IConnector);
var types = AppDomain.CurrentDomain.GetAssemblies()
    .SelectMany(s => s.GetTypes())
    .Where(p => type.IsAssignableFrom(p) && !p.IsInterface).ToList();
foreach (Type typeString in types)
{
    object obj = Activator.CreateInstance(typeString);
    connectors.Add((IConnector)obj);
}
connectionChooserComboBox.DataSource = connectors;

The form shows this, the usercontrol should be shown here Error thrown by


Solution

  • So it sounds like you need to distinguish between Design mode and Runtime Mode.

    There are some things to help you with this, you need to pick which one works best for you;

    There are

    • The DesignMode property
    • LicenseModeUsage property

    The work differently, for example DesignMode does not work in contructors but LicenseMode does. So you need determine what works best for you depending on where you need to use this.

    This post explains it much better than I can; http://dotnetfacts.blogspot.de/2009/01/identifying-run-time-and-design-mode.html