Winforms: TextFormatFlags.NoPadding not working with .Net 5 Windows App (Core).

Created on 2 Sep 2020  路  7Comments  路  Source: dotnet/winforms

  • .NET Core Version:
    .NET SDK 5.0.100-preview.8.20417.9

  • Have you experienced this same bug with .NET Framework?:
    No
    It is also not reproducible with .Net Core 3.1.

Problem description:

The TextRenderer.DrawText method ignores the TextFormatFlags.NoPadding flag and render string with padding. But the MeasureText method works correctly with this flag.
This sample measure text size and render it, when I run this code with the .Net Framework / .Net Core 3.1/ .Net 5 old builds - this code works correctly, but after update VS to Version 16.8.0 Preview 2.1, this code works incorreclty.

Sample:

protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            var g = e.Graphics;
            var text = "Text Text Text";
            var font = Font;
            var width = int.MaxValue;
            var flags = TextFormatFlags.NoPadding;
            var size = TextRenderer.MeasureText(g, text, font, new Size(width, 0), flags);
            var r = new Rectangle(Point.Empty, size);
            TextRenderer.DrawText(g, text, font, r, ForeColor, flags);
            System.Diagnostics.Debug.WriteLine(r);
        }

Result:
.Net Framework 4.5.2:
NetF452
.Net 5:
Net5

Expected behavior:
The TextFormatFlags.NoPadding - Does not add padding to the bounding rectangle.

Minimal repro:

  1. Run attached app:
    DrawText_issue.zip
  2. Observe - NoPadding doesn't work with the DrawText method.
regression

Most helpful comment

Please note that we consider this issue as showstopper. For us it breaks behavior both for pure .Net 5 applications and for .Net 4.5.2 libraries used in .Net 5 apps. Text wraps unexpectedly in wrong places

All 7 comments

Please note that we consider this issue as showstopper. For us it breaks behavior both for pure .Net 5 applications and for .Net 4.5.2 libraries used in .Net 5 apps. Text wraps unexpectedly in wrong places

Investigating...

I know what the problem is, working on the fix.

The fix is in for RC2 (#3873).

Verified this issue with .Net SDK 5.0.100-rc.2.20460.32, this issue is fixed. After run attached app(DrawText_issue.zip),the TextFormatFlags.NoPadding - does not add padding to the bounding rectangle,TextFormatFlags.NoPadding is working with .Net 5 Windows App (Core). Please see below result:
image

Hi guis,
I checked Preview 3, now MeasureText ignored NoPadding too.
sdk - 5.0.100-rc.1.20452.10
1

@smToDo as @Ashley-Li indicated the issue is fixed in SDK 5.0.100-rc.2.20460.32

Was this page helpful?
0 / 5 - 0 ratings