Question

I've written an extended ListView in C# WinForms. It's purpose is to show a list of currently running applications using the WinAPI. However, If I try to populate the ListView in the constructor it works when I run the application, but if you try to put the control on any form it will crash VS 2008.

The reason for the crashing is I believe caused by the use of the ThreadPool or the use of P/Invoke calls. I tried to wrap it in a conditional to check the DesignMode property, but I found out that will always returned false in the constructor. To solve my problem I overrode the InitLayout method of the ListView base class and put my populatation code there, but I don't think this is the best place to put it.

Does anyone know where the best place to put my pre-population code in an extended ListView?

Thanks!

Here's what the fix looks like:

protected override void InitLayout()
{
    if (DesignMode)
        return;

    RefreshApplications();

    base.InitLayout();
}
Was it helpful?

Solution

Use this to distinguish between Design mode and Application running:

if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)
{
}   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top