0

I have a windows form application that has two main panels: The one on the left is a narrow strip which has a series of radio buttons. The panel on the right houses a Tabcontrol, which has multiple Tabpages added to it which the user can select among along the top. Each of these tabpages themselves has about 7 DataGridViews added to it. Each DataGridView has about 5-6 columns of text, with a variable number of rows (10-500). The data added to it was done directly to the DGV itself, ie using the DGV.Rows.Add() method, passing an object array, not via a datasource.

When a user selects a different tab page, the datagridview that gets shown is dependent on the radiobutton that is selected on the left. I accomplish this by handling the SelectedIndexChange event of the tabcontrol and each of the radiobutton's CheckedChange event.

Within the SelectedIndexChange event of the tabcontrol, I programmatically checked the currently selected radioButton. Then within the radiobutton's CheckedChange event, I iterate through all of the DataGridViews on the TabControl's selectedTab and hide all those that don't match the one corresponding to the selected radiobutton.

My issue is everytime the user starts changing among a lot of tabs, or tries to view a DGV that has many rows, the program would throw the following error:

System.ComponentModel.Win32Exxception: Error creating window handle.

Does anyone know what would cause the above error? My initial suspicion was that when I change to a different tabpage, the DGV on the original tabpage i was on was still in memory, but when I try calling .Dispose() on it, the DGV just disappears. It may be I am missing something fundamental here.

jonsca
  • 10,218
  • 26
  • 54
  • 62

1 Answers1

0

The problem is that you are probably trying to show a disposed DataGridView.

Do not dispose any Control in your form unless you are previously removing it from the appropiate Container.Controls collection (because you do not need it anymore and/or you plan adding a new one in its place). Otherwise dispose any controls when closing and disposing the Form that contains them if necessary.

In order to show the proper DataGridView depending on selected tab and user options use the Visible property or dynamically add and remove the needed controls to the container's Controls collection (in this case TabPage.Controls.

If its not that then maybe you have a "control leak" (probably event handler holding the object) and you are exceeding the windows handle limit for any given application (10.000 I think it is).

InBetween
  • 32,319
  • 3
  • 50
  • 90
  • I am actually not calling .Dispose() on any form explicitly so if it is being done, it is being done automatically by the CLR. I was originally thinking I needed to do this because this error seems to crop up more often when there is a lot of information on the DGV, or when the user has had a long session open where he has changed the views many times. – csharp_learner Jun 26 '11 at 17:41
  • @csharp_learner: I was adding info to the answer when you posted the comment. Read last paragraph and check the following related question: [Winforms issue - Error creating window handle](http://stackoverflow.com/questions/222649/winforms-issue-error-creating-window-handle) – InBetween Jun 26 '11 at 17:48
  • Hi InBetweeen, Regarding your third paragraph, I basically call .Hide() on the DGV's which are not supposed to be shown, which is equivalent to setting the .Visible = true/false. I have taken a look at the task manager and noticed the following behavior when i started switching between tabpages: The "Handles" column stayed below 400 at all times. The "USER Objects" column however jumped about 40 each time i switched to a different tabpage. When it hits 300, i start getting the dreaded error above. GDI object count also goes up by 1. Let me know if the above information can assist. Thanks. – csharp_learner Jun 26 '11 at 18:16