Error Creating Window Handle

johnsone006

New member
Joined
May 23, 2025
Messages
1
Programming Experience
3-5
So for the past several days, I have been developing an analogous color scheme program in WinForms. Granted, I did create a whole bunch of buttons to enable the user to choose the base color -759, to be exact. However, I do believe I have done literally everything I can do to properly release the resources in that form:
private void DisposeItems()
{
List<System.Windows.Forms.Control> c = GetAllControls(this);
int count = c.Count;
for (int i = 0; i < count; i++)
{
if (c.GetType() == typeof(System.Windows.Forms.Button))
{
Button btn = (Button)c;
btn.Click -= OnColorButton_Click;
}
c.Dispose();
c = null;
}
this.Controls.Clear();
this.Close();
this.Dispose();
}
I don't know what else to do, besides that, given that list does contain all the controls for that particular form. And yet, I am still getting System.ComponentModel.Win32 exception, native error code 1158. But only when I choose some colors; others work fine. The form where the exception happens has a total of 12 controls. And the error doesn't even happen for a window-it happens for a combo box. A combo box that, by the time the error is thrown, had long since shown up on the form that also showed up fine.
I am confused.
Could anyone point me in the right direction for further troubleshooting this issue please?
 
Windows has a limit on the number of windows and GDI objects that you can create. Why create all those buttons when you can do some hit testing from within a single custom control?

Do you have the full call stack when the exception is thrown?
 
Back
Top Bottom