Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 27, 2025

Fixes #14081

Proposed changes

  • Add IsHandleCreated guard in ParentOverlay() before calling SetParent and SetWindowPos
  • Add IsHandleCreated check in WndProc WM_PARENTNOTIFY handler before calling SetWindowPos on overlay controls

Customer Impact

  • Prevents Visual Studio crashes when custom control properties invoke native window methods during design time
  • Accessing Control.Handle without a created handle forces CreateHandle(), which can crash the designer

Regression?

  • No

Risk

  • Low. Follows existing pattern used throughout WinForms (e.g., Control.BringToFront(), Control.SendToBack())

Test methodology

  • Code review passed
  • CodeQL security check passed
  • Pattern matches existing guards in Control.cs:4406 and Control.cs:10055

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 96bvsblobprodcus338.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/winforms/winforms/.dotnet/dotnet /home/REDACTED/work/winforms/winforms/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/winforms/winforms/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/winforms/winforms/artifacts/toolset/11.0.0-beta.25575.110.txt (dns block)
    • Triggering command: /home/REDACTED/work/winforms/winforms/.dotnet/dotnet dotnet build src/System.Windows.Forms.Design/src/System.Windows.Forms.Design.csproj --no-restore (dns block)
  • rcxvsblobprodcus328.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/winforms/winforms/.dotnet/dotnet dotnet build src/System.Windows.Forms.Design/src/System.Windows.Forms.Design.csproj --no-restore (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Visual Studio Crash When Invoking NativeMethods.SetWindowPos Method</issue_title>
<issue_description>## Environment
Visual Studio professional 2026 - version 18.0.0

.NET version

Facing this issue in .NetCore

Did this work in a previous version of Visual Studio and/or previous .NET release?

No

Issue Description

I have a Boolean property named NativeCheck. During the property processing, the SetWindowPos method is invoked. Visual Studio crashes when this method is executed. Below is the method definition:
[DllImport("USER32.dll");
internal static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags)

Demo Link

CustomControl_Demo.zip

Issue replication video

NativeMethods_Issue.mp4

Steps to reproduce

  1. Open the Designer.
  2. Select the CustomButton and navigate to the Properties window.
  3. Set the NativeCheck property to True.

Expected Behavior: The value should be properly set to the NativeCheck property without any issues.
Observed Behavior: When setting the NativeCheck property to True, Visual Studio crashes.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Microsoft Reviewers: Open in CodeFlow

Co-authored-by: SimonZhao888 <133954995+SimonZhao888@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix Visual Studio crash when invoking SetWindowPos method Add IsHandleCreated checks before calling SetWindowPos in DesignerFrame Nov 27, 2025
Copilot AI requested a review from SimonZhao888 November 27, 2025 01:17
Copilot finished work on behalf of SimonZhao888 November 27, 2025 01:17
@codecov
Copy link

codecov bot commented Nov 27, 2025

Codecov Report

❌ Patch coverage is 18.18182% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.14865%. Comparing base (48f77ea) to head (bfc86fb).

Additional details and impacted files
@@                 Coverage Diff                 @@
##                main      #14086         +/-   ##
===================================================
- Coverage   77.15479%   77.14865%   -0.00615%     
===================================================
  Files           3279        3279                 
  Lines         645317      645323          +6     
  Branches       47718       47720          +2     
===================================================
- Hits          497893      497858         -35     
- Misses        143732      143776         +44     
+ Partials        3692        3689          -3     
Flag Coverage Δ
Debug 77.14865% <18.18182%> (-0.00615%) ⬇️
integration 18.97114% <18.18182%> (-0.01503%) ⬇️
production 52.00506% <18.18182%> (-0.01325%) ⬇️
test 97.40749% <ø> (ø)
unit 49.44660% <0.00000%> (+0.00417%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@SimonZhao888
Copy link
Member

This is not an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Visual Studio Crash When Invoking NativeMethods.SetWindowPos Method

2 participants