diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ComboBox/ComboBox.ComboBoxChildNativeWindow.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/ComboBox/ComboBox.ComboBoxChildNativeWindow.cs index 60b17e60db7..0ff802ec8e9 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/ComboBox/ComboBox.ComboBoxChildNativeWindow.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/ComboBox/ComboBox.ComboBoxChildNativeWindow.cs @@ -25,6 +25,18 @@ protected override unsafe void WndProc(ref Message m) case PInvokeCore.WM_GETOBJECT: WmGetObject(ref m); return; + case PInvokeCore.WM_WINDOWPOSCHANGING: + if (_childWindowType == ChildWindowType.DropDownList) + { + WmWindowPosChanging(ref m); + DefWndProc(ref m); + } + else + { + _owner.ChildWndProc(ref m); + } + + break; case PInvokeCore.WM_MOUSEMOVE: if (_childWindowType == ChildWindowType.DropDownList) { @@ -153,5 +165,23 @@ private unsafe void WmGetObject(ref Message m) throw new InvalidOperationException(SR.RichControlLresult, e); } } + + private unsafe void WmWindowPosChanging(ref Message m) + { + // The native ComboBox sizes the dropdown list during its own layout + // pass (after CBN_DROPDOWN fires). Intercept here to enforce the + // managed computed height before the OS commits the final size. + // This ensures the UI reflects Items.Count (or explicit DropDownHeight) + // even when the list is empty or items are cleared at runtime. + WINDOWPOS* pos = (WINDOWPOS*)(nint)m.LParamInternal; + if (pos is not null && (pos->flags & SET_WINDOW_POS_FLAGS.SWP_NOSIZE) == 0) + { + int height = _owner.GetCalculatedDropDownHeight(); + if (pos->cy != height) + { + pos->cy = height; + } + } + } } } diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ComboBox/ComboBox.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/ComboBox/ComboBox.cs index 5e4422eaf9c..86e36af873c 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/ComboBox/ComboBox.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/ComboBox/ComboBox.cs @@ -3327,14 +3327,8 @@ public override string ToString() return $"{s}, Items.Count: {_itemsCollection?.Count ?? 0}"; } - private void UpdateDropDownHeight() + private int GetCalculatedDropDownHeight() { - if (_dropDownHandle.IsNull) - { - return; - } - - // Now use the DropDownHeight property instead of calculating the Height... int height = DropDownHeight; if (height == DefaultDropDownHeight) { @@ -3343,6 +3337,19 @@ private void UpdateDropDownHeight() height = ItemHeight * count + 2; } + return height; + } + + private void UpdateDropDownHeight() + { + if (_dropDownHandle.IsNull) + { + return; + } + + // Now use the DropDownHeight property instead of calculating the Height... + int height = GetCalculatedDropDownHeight(); + PInvoke.SetWindowPos( _dropDownHandle, HWND.HWND_TOP,