From 30b66c1d0d96ff6f0d35085e3b6b0ebcb818e77c Mon Sep 17 00:00:00 2001 From: "Simon Zhao (BEYONDSOFT CONSULTING INC)" Date: Fri, 29 May 2026 14:13:31 +0800 Subject: [PATCH] Fix MaskedTextBox PropertyGrid text editor DPI scaling at runtime --- .../Design/MaskedTextBoxTextEditorDropDown.cs | 4 ++-- .../MaskedTextBoxTextEditorDropDownTests.cs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/MaskedTextBoxTextEditorDropDown.cs b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/MaskedTextBoxTextEditorDropDown.cs index 54ac9049bb7..a18daad38e7 100644 --- a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/MaskedTextBoxTextEditorDropDown.cs +++ b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/MaskedTextBoxTextEditorDropDown.cs @@ -48,8 +48,8 @@ public MaskedTextBoxTextEditorDropDown(MaskedTextBox maskedTextBox) BackColor = Drawing.SystemColors.Control; BorderStyle = BorderStyle.FixedSingle; Name = "MaskedTextBoxTextEditorDropDown"; - Padding = new Padding(16); - Size = new Drawing.Size(100, 52); + Padding = new Padding(LogicalToDeviceUnits(16)); + Size = LogicalToDeviceUnits(new Drawing.Size(100, 52)); ((System.ComponentModel.ISupportInitialize)(_errorProvider)).EndInit(); ResumeLayout(false); PerformLayout(); diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/MaskedTextBoxTextEditorDropDownTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/MaskedTextBoxTextEditorDropDownTests.cs index 617e9a797ba..a6b14ab10dd 100644 --- a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/MaskedTextBoxTextEditorDropDownTests.cs +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/MaskedTextBoxTextEditorDropDownTests.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Drawing; + namespace System.Windows.Forms.Design.Tests; public class MaskedTextBoxTextEditorDropDownTests @@ -54,4 +56,20 @@ public void MaskInputRejected_SetsError_WhenInputRejected() dropDownMaskedTextBox.Text = "invalid"; errorProvider.GetError(dropDownMaskedTextBox).Should().Contain(SR.MaskedTextBoxHintDigitExpected); } + + [Fact] + public void DropDown_SizeAndPadding_ScalesWithDPI() + { + using MaskedTextBox maskedTextBox = new(); + using MaskedTextBoxTextEditorDropDown dropDown = new(maskedTextBox); + + // The size and padding should be scaled based on DPI + // At 96 DPI (100%), Size should be (100, 52) and Padding should be (16, 16, 16, 16) + // At higher DPI, these values should scale proportionally + Size expectedSize = dropDown.LogicalToDeviceUnits(new Size(100, 52)); + Padding expectedPadding = new(dropDown.LogicalToDeviceUnits(16)); + + dropDown.Size.Should().Be(expectedSize); + dropDown.Padding.Should().Be(expectedPadding); + } }