Shachar
Shachar

Reputation: 21

App crashes when selecting an item in a DataGridView combobox cell if the app is open on an external monitor

I have an app that I wrote a few years ago using Winforms and C# and recently I encountered a weird bug. When selecting an item from a datagridview combobox cell the app is crashing with the error "System.OutOfMemoryException: To many items in combobox" and the stack trace below:

   at System.Windows.Forms.ComboBox.NativeAdd(Object item)
   at System.Windows.Forms.ComboBox.OnHandleCreated(EventArgs e)
   at System.Windows.Forms.Control.WmCreate(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ComboBox.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)

I don't have a desktop computer available, only laptops, and this seems to happen only if the app is open on an external monitor becuase on a built in screen it works fine.

I tried to mark my external monitor as the primary screen so the app will open on it by default because I thought it might have something to do with it but the problem remains the same.

I also tried to add a dummy combobox column to less complex form controller then the one where the problem occurs and the same thing happend.

Update:

I have made some progress. Apparently the bug occours when the scale setting of the external monitor is different than the scale setting of the built in monitor. It will be grate if anyone can explain why this happens and how can I fix it in my app's code.

Sometimes during debugging the error apears immedaitly in the app's main entry point, in those cases I used Visual Studio winforms designer to create lists of items in advance in the combobox columns. In other cases the comboxcells has a data table as a source and the datagridview has it's own data table as a source. so after selecting an item I sync them manually with the following function and the bug happens after calling DataGridView.EndEdit():

        private void DGVAndDataSourceSync(DataGridViewComboBoxEditingControl senderCB, int dgvDtColumn)
        {
            try
            {
                DataRow drCBItemSource = ((DataTable)senderCB.DataSource).Rows[senderCB.SelectedIndex];
                ((DataRowView)senderCB.EditingControlDataGridView.CurrentRow.DataBoundItem).Row[dgvDtColumn] = drCBItemSource[DT_KEY_FIELD_COLUMN];
                _ = senderCB.EditingControlDataGridView.EndEdit();
            }

            catch (Exception ex) { throw ex; }
        }

Second Update

I continued experimenting with my app and anouther demo one I made with nothing but a datagridview containg only a combobox column. I discovered that when I go to the following lines in my app.manifest:

      <!-- Windows 10 -->
      <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />>

and comment out the line with the supportedOS tag the bug is gone. I don't understand why this is happing and it will be grate if someone knows to explain it

Upvotes: 1

Views: 215

Answers (1)

pm100
pm100

Reputation: 50210

this is nothing to do with out of memory

here is the code that throws throws this error

      int insertIndex = unchecked( (int) (long)SendMessage(NativeMethods.LB_ADDSTRING, 0, GetItemText(item)));
      ...
      if (insertIndex == NativeMethods.LB_ERR) {
            // On some platforms (e.g. Win98), the ListBox control
            // appears to return LB_ERR if there are a large number (>32000)
            // of items. It doesn't appear to set error codes appropriately,
            // so we'll have to assume that LB_ERR corresponds to item
            // overflow.
            //
            throw new OutOfMemoryException(SR.GetString(SR.ListBoxItemOverflow));
        }

An article I posted in a comment says that this is caused by a item returning null when tostring is called during the call to GetItemText

https://www.csharp411.com/combobox-exception-too-many-items-in-the-combo-box/

Upvotes: 0

Related Questions