Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Error creating window handle when adding controls

Posted on 2011-03-15
4
Medium Priority
?
1,130 Views
Last Modified: 2012-05-11
Hi

I am developing a VB.NET application in VS2008. It creates a large number of controls on the form and in the forms containers. When the number of controls reacech several thousends the program abends with the error: "Error creating window handle". Tests I have done indicates there is a max number of controls that can be added.

Is there a safe way to increace the max number of handels?
Or is it safer to rewrite the program to avoid so manny controls simultainiously?

My test appl looks like this:

Public Class Form1
    Dim lbl() As Label
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim X As Integer
        ReDim lbl(10000)
        For X = 1 To 10000
            lbl(X) = New Label
            lbl(X).Text = X
            lbl(X).Location = New Point(100, 20 * X)
            lbl(X).AutoSize = True
            Me.Controls.Add(lbl(X))
        Next
    End Sub
End Class

The full error report is attached. Error-creating-window-handle.txt
0
Comment
Question by:daghoff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 34

Accepted Solution

by:
it_saige earned 1000 total points
ID: 35140313
Here is a great article about this problem.  The author even goes into some resolutions but states essentially that, ultimately it would be better to reduce the number of controls in order save system resources.  

http://weblogs.asp.net/fmarguerie/archive/2009/08/07/cannot-create-window-handle-desktop-heap.aspx

HTH,

-saige-
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35140835
10000 labels?!...

What are you displaying to the user?

A DataGridView might be a better choice:
http://msdn.microsoft.com/en-us/library/e0ywh3cz.aspx
0
 

Author Comment

by:daghoff
ID: 35155329
I know it may sound a bit crazy, but initially it worked fine. The problem is that the number of controls generated is dependent of user input. In some cases to many controls are created. After having read the provided article I have decided to rewrite the program. A DataGridView is not what I need because I must design the UI in detail.

But one last question: How do you properly release ALL resources that a control on a form may have allocated, when the control is no longer needed? So far I have only used a "Controls.Remove" statement on the control or on the control's container. Is this enough or should I add in some dispose/release/close commands? Are there other things to do like using a garbage collector?
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1000 total points
ID: 35155777
Right...calling Dispose() on the control will help reclaim the memory faster.
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

cPanel is a Unix based web hosting control panel that provides a graphical interface and automation tools designed to simplify the process of hosting a web site. cPanel utilizes a 3 tier structure that provides functionality for administrators, rese…
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question