dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.36k stars 968 forks source link

Label.AutoSize bug #9549

Open mikola-akbal opened 1 year ago

mikola-akbal commented 1 year ago

.NET version

.NET 7

Did it work in .NET Framework?

Yes

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

No

Issue description

When you use property Label.AutoSize = true, calculating of Label size is wrong. It seems, .NET code uses default font instead of Label's font. If you set for Label big font, auto size of Label will be smaller then must be.

It can be connected with font scaling. I remember, in .NET Core 3 this feature had real displaying bugs. That was reason why I decided to stay with old good .NET Framework. It seems this font scaling still has bugs.

It is connected by some way with relationship between control and container. Let me explain. When Label is already in Form (is in the collection Form.Controls and is displayed) - AutoSize is OK, size is calculated correctly. But when Label exists yet separately (is not yet added to Form), AutoSize calculates wrong size. If I set bigger font for this separate Label, I see smaller auto size that it must be. It looks like ignoring of property Label.Font that I set.

Steps to reproduce

1) Create windows form in VS constructor.

2) Create separate Label programmatically (not in constructor).

3) Set Label.AutoSize = true.

4) Set some text for Label.

5) Log size of Label.

6) Try to set another fonts for this Label (with different font sizes) and log size of Label.

7) Do all of that before adding Label to the form. Or even without adding at all.


With constructor, Label.AutoSize is OK. But when you need to create Label separately and know it's size - you get a bug with sizes.

elachlan commented 1 year ago

@Olina-Zhang could your team please test this?

Olina-Zhang commented 1 year ago

@mikola-akbal I tried to repro this issue with following code: new a label in an event, log label's Size before and after changing its Font, the result is same(it should be different). Is it your issue? This is not a regression issue, also reproduces in .net framework 4.8.1. image

mikola-akbal commented 1 year ago

I put reproducing repository here:

https://github.com/mikola-akbal/Label-AutoSize-Bug

This is screen-save:

Measure

You see that AutoSize calculates Width of Label to 100. But in fact it's wider: at least 105.

Maybe, it is not wrong font. It can be simply omitting of calculation. Number 100 looks like default.

The problem is wrong positioning of controls. You see that - because of wrong calculation of width - Label is placed over TextBox.

If you try to change size of Form and Panel - you see that size of Label is calculated correctly.