2

I've a UserControl containing a FlowLayoutPanel. A lot of this control instances are needed to be used in a nested form on the Form. I found it out that only 15 nested instances can be created! So I decided to check a more simple model of my control out. The model consist of a GroupBox that contains a Panel:

Control parent = this;
for (int groupIndex = 0; groupIndex < 100; groupIndex++)
{
    GroupBox grp = new GroupBox();
    Panel pnl = new Panel();
    pnl.Dock = DockStyle.Fill;
    pnl.Parent = grp;

    grp.Parent = parent;
    grp.Size = new Size(parent.Width - 10, parent.Height - 10);

    parent = pnl;
}

When groupIndex reaches to 24, it encounters the Error creating window handle exception. What's the reason for and how to overcome it?

Thanks

Mehdi
  • 2,194
  • 2
  • 25
  • 39
  • 1
    You should check out [Pushing the Limits of Windows: Handles](http://blogs.technet.com/b/markrussinovich/archive/2009/09/29/3283844.aspx) – Conrad Frix Apr 15 '12 at 02:34
  • See if this [forum post](http://social.msdn.microsoft.com/forums/en-us/winforms/thread/84F3FFBE-DA0B-43C9-8565-2BDA39003655) and this [Bing search](http://www.bing.com/search?q=Error+creating+window+handle+exception&qs=n&form=QBLH&pq=error+creating+window+handle+exception&sc=0-0&sp=-1&sk=) shed some light on the problem. – Mark Hall Apr 15 '12 at 02:36
  • 1
    Do you have a good reason for nesting things this deep? – Ry- Apr 15 '12 at 02:38
  • @ConradFrix: Thanks-- I read Mark Russinovich article. As he mentioned _There are two limits related to the number of handles a process can create: **1-** the maximum number of handles the system sets for a process and **2-** the amount of memory available to store the handles and the objects the application is referencing with its handles._ But none of them is hold here! isn't? – Mehdi Apr 15 '12 at 02:49
  • @minitech: Yeah! The UI is used in a management application and should be able to contain a series of procedure charts in a nested form. – Mehdi Apr 15 '12 at 02:51
  • @Mimi: Hm... okay. Tough luck! Is drawing anything using GDI+ to remove a layer of controls possible? – Ry- Apr 15 '12 at 02:53
  • @minitech :) It's possible but turns some of my application features off. (e.g. my controls are furnished by drag & drop ability, so they can easily be nested.) – Mehdi Apr 15 '12 at 03:05
  • @MarkHall: Thanks-- I checked it. As far as I searched, such a problem is usually due to maximum number of handles which doesn't seem to be true here. – Mehdi Apr 15 '12 at 03:12

1 Answers1

1

This a duplicate of the following question:

Control Nesting Limits in WinForms

To answer your question, Windows has a limit of 50 nested controls (source).

Your sample creates a group box and nests a panel inside of it, and you do this 24 times before the crash occurs. If you account for the form itself, you are hitting that limit.

The limit is enforced when the controls are drawn. From my testing, I was able to create and nest many hundreds of controls, but when I added the top level control of that nest to a visible control, it still crashed.

Community
  • 1
  • 1
KyleK
  • 190
  • 13