Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
1. Winform User control has two panels A, B. A docked to left, B docked to fill.
2. Panel A has two buttons (no events), panel B has two label and two TextBoxes (no events).
3. Adding user control to a Form using given lines, this form has no other control.

public partial class Form1 : Form
    {
        private UserControl1 userControl;
        public Form1()
        {
            InitializeComponent();

            for (int i = 0; i <1000; i++)
            {
                userControl = new UserControl1();
                userControl.Dock = DockStyle.Top;
                Controls.Add(userControl);
            }
        }
    }

4. Given below error occurs at line
Application.Run(new Form1());
, any solution to add large number of user controls to a form?

System.ComponentModel.Win32Exception
  HResult=0x80004005
  Message=Error creating window handle.
  Source=System.Windows.Forms
  StackTrace:
   at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
   at System.Windows.Forms.Control.CreateHandle()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


What I have tried:

looking for solution to resolve this issue?
Posted
Updated 23-Jan-19 10:27am
v4
Comments
MadMyche 23-Jan-19 15:31pm    
Why do you need so many controls?
[no name] 23-Jan-19 15:34pm    
I just want it, I do not see a reason why I can`t have. Do you have any thought to achieve it? regardless of how imperfect the design is...
Richard MacCutchan 23-Jan-19 15:36pm    
You have run out of system resources. There is no valid reason to add that many controls to a form; you need to rethink your design.
[no name] 23-Jan-19 15:43pm    
Process Memory stays at 37 MB, CUP uses less than 10<.
I am not an expert but I ran GB size of games on this pc, why this little app is crying?
Richard MacCutchan 23-Jan-19 15:54pm    
You have run out of Windows handles most likely. Stop wasting time with this.

1 solution

Actually, the error does not quite happen at line "Application.Run(new Form1());". If you were to step IN TO that line, you'd see it is hitting your loop. The error likely means that you have run out of handles. However, you can google the error for other options.

I would write your loop variable (i) out to the debug window so that you can see exactly when it happens. If it happens after 10 iterations, something else is likely going on. But if it is close to 1000, then for sure you are running out of handles.
 
Share this answer
 
v3
Comments
[no name] 23-Jan-19 17:06pm    
A 5 for the response.

"Out of handles close to 1000": I think it is important what kind of handles. Skype and a lot of browsers do usually show more than 1000 handles in task manager. I think the problem is on the GDI Objects...
ZurdoDev 23-Jan-19 17:11pm    
I just looked closer at the code and OP is creating an infinite loop.
[no name] 23-Jan-19 17:22pm    
I don't see an infinite loop ...?

But I think a very big unkown is also "UserControl1", we do not know what "UserControl1" is doing and wheter it is correct.
[no name] 23-Jan-19 19:23pm    
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
}

This is all, UserControl1 doing nothing, as described in question, this user control has panels, buttons, labels and text boxes (details mentioned in question) and no other step at all.
[no name] 23-Jan-19 19:24pm    
There is no infinite loop.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900