Sunday, April 3, 2011

What method or event should I use prepopulate an extended ListView?

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();
}
From stackoverflow
  • Use this to distinguish between Design mode and Application running:

    if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)
    {
    }
    

0 comments:

Post a Comment