Winforms: Exception occurred when add DataGridView editing controls to other control in .Net Core

Created on 21 Oct 2019  路  6Comments  路  Source: dotnet/winforms

.Net Core version: 3.0.1
OS version: win10 64-bit

Please try my sample, when add MS control to Panel, there's always an exception occurred:

///
/// exception occurred when they are added to Panel, I believe there're more MS controls have same issue.
///

///this.panel1.Controls.Add(new ToolStripDropDown());
//this.panel1.Controls.Add(new ToolStripDropDownMenu());
//this.panel1.Controls.Add(new ContextMenuStrip());
//this.panel1.Controls.Add(new ToolStripDropDown());
//this.panel1.Controls.Add(new DataGridViewTextBoxEditingControl());
this.panel1.Controls.Add(new DataGridViewComboBoxEditingControl());
System.NullReferenceException: Object reference not set to an instance of an object.
  at System.Windows.Forms.DataGridViewComboBoxEditingControl.OnHandleCreated(EventArgs e) in /_/src/System.Windows.Forms/src/System/Windows/Forms/DataGridViewComboBoxEditingControl.cs:line 164
   at System.Windows.Forms.Control.WmCreate(Message& m) in /_/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs:line 12452
   at System.Windows.Forms.Control.WndProc(Message& m) in /_/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs:line 13486
   at System.Windows.Forms.ComboBox.WndProc(Message& m) in /_/src/System.Windows.Forms/src/System/Windows/Forms/ComboBox.cs:line 4051
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) in /_/src/System.Windows.Forms/src/System/Windows/Forms/Control.ControlNativeWindow.cs:line 65
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) in /_/src/System.Windows.Forms/src/System/Windows/Forms/Control.ControlNativeWindow.cs:line 113
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) in /_/src/System.Windows.Forms/src/System/Windows/Forms/NativeWindow.cs:line 626

I checked the source code, this bug dues to some internal control override OnHandleCreated method, but didn't check if parent control exists or not, see below:

        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            dataGridView.SetAccessibleObjectParent(this.AccessibilityObject);
        }

InternalControlTest.Core.zip

bug regression

All 6 comments

@Lily721 thank you for reporting. I don't think it is a totally valid use-case to add DataGridViewComboBoxEditingControl to controls outside DataGridView.
You're welcome to send to PR with a fix 馃槈

@M-Lipin Looks like regression from 31ad7eec1780d1a79f2df04f21b18286e298026d in #1524.

@Vino-Wang could you please check whether we have any more issues of similar nature?

Not high priority but people cache controls by reparenting them to a dummy control while not in use, accidently creating the handle probably shouldn't throw in this scenario.

Hi @RussKie, we have verified a number of controls added to form by using customer project code, this issue can reproduce with some specific controls and cannot reproduce with other common controls in Toolbox. Please see the following table details.

We didn鈥檛 encounter the similar issues for now.
Table

Thank you @Vino-Wang

@M-Lipin it is all yours.

Sorry, changed the name to reflect the issue with EditingControl added as a child control to not proper parent (other than DataGridView)

Verified this bug with .Net Core 3.1.100-preview3-014638 from Release branch, this bug is fixed, there is no exception when add DataGridView editing controls to other control in .Net Core.

Was this page helpful?
0 / 5 - 0 ratings