dotnet / winforms

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

dataGridView doesn't respect font settings if new form's Font != DefaultFont #3033

Closed kirsan31 closed 4 years ago

kirsan31 commented 4 years ago

---UPD--- Clarifications: https://github.com/dotnet/winforms/issues/3033#issuecomment-689333353

Problem description: dataGridView doesn't respect font settings in Styles (DefaultCellStyle, ColumnHeadersDefaultCellStyle...): Snipaste_2020-04-02_17-00-10

Expected behavior: dataGridView must respect font settings.

Windows 7 x64.

Minimal repro: WinFormsCoreTest3.zip

RussKie commented 4 years ago

Is this a blocker? If so, what is the impact? We need to have a good justification for the servicing team.

kirsan31 commented 4 years ago

@RussKie

Is this a blocker? If so, what is the impact? We need to have a good justification for the servicing team.

Yes of course! Grid control is totally broken - you can't customize fonts!!! In migration (to core) terms: every grid with custom fonts (in our apps it's all of them - 100%) are broken.

Lydia-Shi commented 4 years ago
  1. Framework project Set DataGridView ColumnHeadersDefaultCellStyle , run project and DataGridView Style show correctly. Then modify the Form font in Properties Window, Form1.Desginer. cs will be updated and influence DataGridVeiw style Settings, as follows: Framework
  2. Core project Copy the DataGridView ColumnHeadersDefaultCellStyle Settings from the Framework project, run project and DataGridView Style show correctly. Then add code β€œthis.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));” in Form1.Desginer.cs to change the Form Font, run project and DataGridView does not display the style of setting, result as below: Core11
kirsan31 commented 4 years ago

Confirmed - setting the form font breaks dataGridView fonts... Due to the change of the default font in core, we had to explicitly set the font in each form during migration.

SergeySmirnov-Akvelon commented 4 years ago

Hi @RussKie . I have reproduced this issue on .NET Framework (4.7.1 and 4.8) and .NET Core (3.1 and 5.0). Issue is reproduced when Form's file contains next code: this.Font = new System.Drawing.Font(...) CTI team also reproduced it on .NET Framework project (https://github.com/dotnet/winforms/issues/3033#issuecomment-610882306). I asked CTI team verify it and confirm. If this is confirmed, then we must remove the regression label

weltkante commented 4 years ago

If this is confirmed, then we must remove the regression label

I'd still consider fixing this important (and arguably a regression), since porting applications to .NET Core currently requires setting the font and thus triggering the bug, while on Desktop you could get away with not setting it.

Alternatively implement #3001 then fixing this issue is (probably?) not so important anymore.

kirsan31 commented 4 years ago

@SergeySmirnov-Akvelon

Form's file contains next code: this.Font = new System.Drawing.Font(...)

This is not exactly the truth. Strictly speaking the problem appear when the form Font != DefaultFont. So this line

this.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));

will not break .net Framework version. And this:

this.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);

will not break .net core version.

@weltkante

Alternatively implement #3001 then fixing this issue is (probably?) not so important anymore.

I am afraid that this will not help :( ...

Also, during testing, I found that changing the form's Font - will delete ColumnHeadersDefaultCellStyle and RowHeadersDefaultCellStyle :flushed: ![DGV](https://user-images.githubusercontent.com/17767561/87664970-d1e50a80-c76e-11ea-9047-9cf2188f15e1.gif) Intresting to check this in new core designer...
RussKie commented 4 years ago

Also, during testing, I found that changing the form's Font - will delete ColumnHeadersDefaultCellStyle and RowHeadersDefaultCellStyle

Please raise in the designer repo. /cc: @merriemcgaw

kirsan31 commented 4 years ago

@RussKie I have one more original designer bug for dataGridView 😏 Can I do something or wait for new designer?

RussKie commented 4 years ago

@kirsan31 sorry, that comment was meant for @SergeySmirnov-Akvelon, I thought he posted that. I totally understand the frustration, I too as a user of Windows Forms frustrated by deficiencies in the code base and the lack of 100% functioning designer. The team is working hard to deliver the best possible experience, but it is a fundamentally very laborious and complex task, and it takes time. Unfortunately there is only so many hours in a day, and nine pregnant women won't deliver a baby in one month...

I'd still consider fixing this important (and arguably a regression)

Since this behaviour exists in .NET Framework, this is not a regression. Obviously it manifests itself due to https://github.com/dotnet/winforms/pull/656, which set the new default font. Sadly no-one anticipated such a far reaching impact of that change. We will certainly fix the issue, however since it is not a regression, the priority and the time frame are TBD.

SergeySmirnov-Akvelon commented 4 years ago

Also, during testing, I found that changing the form's Font - will delete ColumnHeadersDefaultCellStyle and RowHeadersDefaultCellStyle

@RussKie, @kirsan31 , the https://github.com/dotnet/winforms-designer/issues/2153 is filed to address the this issue. /cc: @merriemcgaw

dreddy-work commented 4 years ago

My understanding align with what @kirsan31 said above (appear when the form Font != DefaultFont). Default winforms applications do not write the 'Font' statement in to the code file. This is going to be a visible issue in core for the applications that set the default(classic framework) font explicitly in the code. Overriding DGV font ( i think this is not limited to DGV only) with Forms font was original implementation and any generic change here would need be designed by keeping other controls in mind.

@kirsan31 , I want to understand little bit more on your broken application scenario. If you remove the explicit default Font statement from the Forms initialization code, my understanding is, you should be able preserve DGV custom fonts. Is this something you already tried and did not work?

FYI - as this is winforms runtime issue, I will have to close the issue opened in the designer repo. We will track it in this repo.

kirsan31 commented 4 years ago

@dreddy-work

I want to understand little bit more on your broken application scenario. If you remove the explicit default Font statement from the Forms initialization code, my understanding is, you should be able preserve DGV custom fonts. Is this something you already tried and did not work?

Yes, your understanding is right. BUT, due to the change of the default font in core, to achieve form look like it was designed, we need to set forms' font any way :( And even if you will do it in runtime (when form already visible), it will break all dataGridView fonts.

But for us, the problem magically solved (for now) because all our users switched to hi dpi monitor's :) And .net core app look with default system font (forms and controls are sized about 27% larger) is good with 100% DPI :) In .net framework version they use 125% DPI.

FYI - as this is winforms runtime issue, I will have to close the issue opened in the designer repo. We will track it in this repo.

Subj defiantly is winforms runtime issue, but issue opened in the designer repo must be Also, during testing, I found that changing the form's Font - will delete ColumnHeadersDefaultCellStyle and RowHeadersDefaultCellStyle (see bottom of linked message). And it's defiantly designer problem.

Also, i have no answer for this question :

I have one more original designer bug for dataGridView smirk Can I do something or wait for new designer?

RussKie commented 4 years ago

I have one more original designer bug for dataGridView 😏 Can I do something or wait for new designer?

Sorry, I missed it. The best way to report the designer bugs via the VS Feedback - it collects necessary telemetry for the team to triage. We look at all reports, and some bugs may have workarounds, while others have to be prioritised for a fix.

kirsan31 commented 4 years ago

@RussKie

The best way to report the designer bugs via the VS Feedback - it collects necessary telemetry for the team to triage. We look at all reports, and some bugs may have workarounds, while others have to be prioritised for a fix.

Thank your, I will wait for dataGridView designer in core and check it there...

developercommunity.visualstudio.com No way sorry, it's disaster to post and track there. It's not only my opinion, but all of the developers I know. It is almost impossible for a issue opened there to reach its destination :(
RussKie commented 4 years ago

Use this: image

Whilst it may not be perfect it helps routing issues to responsible teams. There are dozens of teams and hundreds of engineers working on VS and .NET world-wide.

It is almost impossible for a issue opened there to reach its destination :(

I don't believe it is (entirely) true. Issues with Windows Forms and the Designer are routed to our team.

kirsan31 commented 4 years ago

Issues with Windows Forms and the Designer are routed to our team.

You have convinced me - I will try (just out of love for WinForms :))) )...

RussKie commented 4 years ago

Once you'll have raised it feel free to email or twitter DM a link to me.

merriemcgaw commented 4 years ago

Sometimes there are routing mistakes, but if you are able to give us the link to the issue created, we can jump on it and make sure it ends up on our radar right away.

kirsan31 commented 4 years ago

@RussKie @merriemcgaw Done: https://developercommunity.visualstudio.com/content/problem/1143911/winforms-datagridview-styles-designer-bugs.html Sorry for delay, had many troubles recently due to 16.7 breaking designer...

merriemcgaw commented 4 years ago

@kirsan31 - I'm checking on the 16.7 issue now. We will update the ticket as soon as we know anything.

merriemcgaw commented 4 years ago

Closing this runtime issue. Keep an eye on your two designer feedback tickets, they are now being looked at. We'll keep you posted there.

kirsan31 commented 4 years ago

@merriemcgaw

Closing this runtime issue. Keep an eye on your two designer feedback tickets, they are now being looked at. We'll keep you posted there.

Sorry, but I am not understand why you closed this? This is separate issue and not designer related.

The problems here (appear only if new form's Font != DefaultFont) are:

So, the only way to make it work:

  1. Set font of the form before DataGridView Styles set.
  2. Set Styles of the DataGridView after this.Controls.Add(this.dataGridView1);.

And yes, strictly speaking this is not regression, but in core it's become more valuable because of def. font changed...

merriemcgaw commented 4 years ago

Shoot - I'm sorry. I must have gotten my wires crossed. I'll reopen. I don't know if we have time to get it into 5.0 RC2 or not, but we can investigate the runtime portion. I'll also update the title to make it clear. Sorry for the confusion!

RussKie commented 4 years ago

@kirsan31 I've run your repro both from a cli and from VS 16.7.1, and both net472 and netcoreapp3.1 render the same, so this doesn't look like a regression: image

What am I missing?

kirsan31 commented 4 years ago

@RussKie You are missing nothing ;) Full explanation in this comment.

The bug persist in both .net and core (must have Font != DefaultFont). But in .net very few ppl. can catch it, since there was no obvious need to change the font of the form (Font == DefaultFont). But in core during migration process from net we immediately caught it when changed form font to .net default.

RussKie commented 4 years ago

Yes, I eventually arrived to the appreciation of the issue. Our code hasn't changed since .NET Framework: https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/DataGridViewMethods.cs,15585. This implementation is what's causing the problem - upon change of font (e.g. form's font changed) we're replacing the configured styles.

kirsan31 commented 4 years ago

Yea I see. And OnFontChangedcalling too in this.Controls.Add(this.dataGridView1);... Good job :+1:

Zheng-Li01 commented 4 years ago

Verified the issue with .NET SDK 5.0.100-rtm.20513.7 that the issue has been fixed that in runtime, the font of DataGridView ColumnHeaders, Cell, Rowheaders is changed as design time