dotnet / dotnet/winforms

Combobox height does not get scaled correctly when dynamically added to a panel

Open
#10,890 3 comments 0 reactions 0 assignees View on GitHub
area-HDPI area-HDPI-PMv2
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
20h 23m
Merged PRs (30d)
103

Description

### .NET version

8.0

### Did it work in .NET Framework?

Not tested/verified

### Did it work in any of the earlier releases of .NET Core or .NET 5+?

_No response_

### Issue description

The height of a combobox control is not scaled correctly when it is dynamically added to a panel after it previously already received a `DpiChanged`-Event and `Application.SetHighDpiMode(HighDpiMode.PerMonitorV2)` is used.

![GitHub-Issue](https://github.com/dotnet/winforms/assets/5320760/c1f53e72-7233-4b71-b41f-7655db4c98b7)

If I manually call `RescaleConstantsForDpi(96, DeviceDpi);` after adding the control to the panel, it does get scaled correctly but I am not sure whether this is a stable workaround.

```cmd
Before RescaleConstantsForDpi: {Width=171, Height=24}
After RescaleConstantsForDpi: {Width=171, Height=33}
```

![GitHub-Issue-2](https://github.com/dotnet/winforms/assets/5320760/2fbbd38d-d206-4d98-a65e-7b4f0edca668)

### Steps to reproduce

Form with a button and a panel. When the button is pressed add the combobox to the panel controls.

![image](https://github.com/dotnet/winforms/assets/5320760/3a55fd71-819b-4fe1-b601-546589f69e2e)

```csharp
public partial class MinimalRepro : Form
{
private ComboBox combobox;

public MinimalRepro()
{
InitializeComponent();

combobox = new ComboBox();
combobox.Items.AddRange(new object[] {
"Test 1",
"Test 2"
});
combobox.Text = "Test2";
}

private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
panel1.Controls.Add(combobox);

// This hack fixes it!
// RescaleConstantsForDpi(96, DeviceDpi);
}
}
```

1. Show the form on the primary monitor (96 DPI)
![image](https://github.com/dotnet/winforms/assets/5320760/abbcf062-5faa-4f5d-90fa-45d7aa0a6bf8)
2. Click the button
![image](https://github.com/dotnet/winforms/assets/5320760/a67737ef-a436-46a7-a895-413d5a262d75)
3. Move the form to the secondary monitor (144 DPI) => OK!
![image](https://github.com/dotnet/winforms/assets/5320760/aa3febd5-4580-43de-8085-176424a2095a)
4. Click the button one or more times => The Combobox height is changed on the first click and wont be correct afterwards. 💥
![image](https://github.com/dotnet/winforms/assets/5320760/ee3be8a6-64b9-4dac-8777-e82844fe55fc)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.