You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
The code generated by the ListBox control is not consistent with Framework #4463
Have you experienced this same bug with .NET Framework?:
No
Problem description:
The generated code for ListBox1 have extra code: this.listBox1.ItemHeight = 15;
Expected behavior:
The generated code for ListBox1 should not have this code: this.listBox1.ItemHeight = 15;
Minimal repro:
Create a Core winforms application.
Add a ListBox control to form.
Double click the Form1.Designer.cs file in Solution Explorer.
Observe the generated code of the ListBox.
Result: The generated code for ListBox1 should not have this code: this.listBox1.ItemHeight = 15;
Note from Dustin: ListBox.ItemHeight property. 4bca5b3/src/System.Windows.Forms/src/System/Windows/Forms/ListBox.cs#L596-L616
It has a [DefaultValue(DefaultItemHeight)] attribute and DefaultItemHeight is 13.
Note from Tanya: Our default value is not accurate anymore due to the default font change. 13 is the default for Microsoft Sans Serif, 8.25pt, 15 is what comctl32 listbox calculates for Segoe UI, 9pt. To confirm - change the parent control's font.
/// The default item height for an owner-draw ListBox. The ListBox's non-ownderdraw
/// item height is 13 for the default font on Windows.
/// </summary>
publicconstintDefaultItemHeight=13;
I don't think we can divorce ItemHeight from DefaultItemHeight either, as it would mess with the designer, and create a mental disconnect for a developer.
The problem is that the current ItemHeight behavior is broken:
if you manually assign the item height value 13 in the designer UI because you want a smaller height then it won't be serialized, instead you get the dynamically derived value of 15 at runtime or when you reopen the designer.
if you want the default size of the font inherited at runtime you can't get it, every time you open the designer it will force 15 on you (instead of leaving it unassigned).
This will in particular disturb user controls which are developed to be flexible, expecting to inherit the font from whatever they are placed on. By letting the designer write fixed 15 you remove the capability of dynamic default values depending on the assigned font.
I would argue that it is not possible to be non-breaking here, by keeping the behavior as it currently is you are also breaking semantics of existing code whenever you open the designer.
If this were Desktop Framework the best solution would be to teach the designer to not write 15 when its meant to be the default value. This might involve changing the attribute on ItemHeight (without changing DefaultItemHeight if desired, which is still a breaking change, but the "most correct" breaking change I can imagine) and perhaps marking the DefaultItemHeight const obsolete. If you want to do it entirely without breaking changes you'd have to teach the designer that the DefaultValue attribute on ItemHeight is incorrect (possible through the TypeDescriptor infrastructure).
@RussKie , could you please explain why it's a problem to require apps to recompile.
The issue, as described applies, to apps in active development. Apps that misuse public constant and expect it to be a certain value, rather than to represent the default height, should be re-done. Apps that expect this constant to contain the default height, will not get the fix unless they recompile, but they have to re-write the code otherwise.
This issue is blocking porting well-written apps from the desktop, as described in @weltkante's second point.
Looks like I missed an important word in my previous response - "I don't think there's a good fix for this."
Since ItemHeight is dependent on the default font, and the default can change, I don't think hardcoding it in a public const is good idea (exposing a public const is never a good idea IMO).
// Changing the item height might require a resize for IntegralHeight list boxes
//
if(IntegralHeight)
{
SizeoldSize=Size;
Size=newSize(oldSize.Width+1,oldSize.Height);
Size=oldSize;
}
EndUpdate();
}
}
}
As a fix I propose the following:
Remove DefaultValue attribute here, and in other places, where a property value depends on the default font metrics.
Fix the serialization mechanism, and only serialize/reset in owner-drawn scenarios.
Mark DefaultItemHeight as obsolete, because it is not a constant.
Replace DefaultItemHeight at all call sites with a new private const set at 16, which is the default value in the native ListBox implementation (SYSFONT_CYCHAR 16)
Fix the serialization mechanism, and only serialize/reset in owner-drawn scenarios.
Just to be clear, this is already supported and just requires removing the DefaultValue attribute (as stated) and instead implementing ShouldSerializeItemHeight and ResetItemHeight methods (which will be picked up by the TypeDescriptor/PropertyDescriptor infrastructure used by property grids).
Just to be clear, this is already supported and just requires removing the DefaultValue attribute (as stated) and instead implementing ShouldSerializeItemHeight and ResetItemHeight methods (which will be picked up by the TypeDescriptor/PropertyDescriptor infrastructure used by property grids).
@MandiMan hello. Could you please tell me how have you reproduced the issue on the main branch? I don't have extra code for some reason.
@Tanya-SolyanikShouldSerializeItemHeight() uses ItemHeight property. This property checks for DrawMode. I've tried additional checks in ShouldSerializeItemHeight(), but it looks like it has the same logic as property itself.
Verified on .NET 9.0.100-alpha.1.24068.28 + latest dlls from Winforms Repo of main branch, it was fixed. The generated code for ListBox1 doesn't have this code: this.listBox1.ItemHeight = 15;
Activity
RussKie commentedon Jan 27, 2021
@DustinCampbell @Tanya-Solyanik I don't think there's a fix for this.
We can't change
DefaultItemHeightbecause it is a public const, any library/app that has referenced it must be recompiled.winforms/src/System.Windows.Forms/src/System/Windows/Forms/ListBox.cs
Lines 52 to 56 in 4bca5b3
I don't think we can divorce
ItemHeightfromDefaultItemHeighteither, as it would mess with the designer, and create a mental disconnect for a developer.winforms/src/System.Windows.Forms/src/System/Windows/Forms/ListBox.cs
Lines 601 to 605 in 4bca5b3
weltkante commentedon Jan 27, 2021
The problem is that the current
ItemHeightbehavior is broken:13in the designer UI because you want a smaller height then it won't be serialized, instead you get the dynamically derived value of15at runtime or when you reopen the designer.This will in particular disturb user controls which are developed to be flexible, expecting to inherit the font from whatever they are placed on. By letting the designer write fixed
15you remove the capability of dynamic default values depending on the assigned font.I would argue that it is not possible to be non-breaking here, by keeping the behavior as it currently is you are also breaking semantics of existing code whenever you open the designer.
If this were Desktop Framework the best solution would be to teach the designer to not write 15 when its meant to be the default value. This might involve changing the attribute on
ItemHeight(without changingDefaultItemHeightif desired, which is still a breaking change, but the "most correct" breaking change I can imagine) and perhaps marking theDefaultItemHeightconst obsolete. If you want to do it entirely without breaking changes you'd have to teach the designer that theDefaultValueattribute onItemHeightis incorrect (possible through the TypeDescriptor infrastructure).Tanya-Solyanik commentedon Jan 27, 2021
@RussKie , could you please explain why it's a problem to require apps to recompile.
The issue, as described applies, to apps in active development. Apps that misuse public constant and expect it to be a certain value, rather than to represent the default height, should be re-done. Apps that expect this constant to contain the default height, will not get the fix unless they recompile, but they have to re-write the code otherwise.
This issue is blocking porting well-written apps from the desktop, as described in @weltkante's second point.
RussKie commentedon Jan 28, 2021
Looks like I missed an important word in my previous response - "I don't think there's a good fix for this."
Since
ItemHeightis dependent on the default font, and the default can change, I don't think hardcoding it in a public const is good idea (exposing a public const is never a good idea IMO).winforms/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs
Lines 1785 to 1801 in 26ee7b5
winforms/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs
Lines 11461 to 11468 in 26ee7b5
Also I am not sure how significant this is problem is though:
ItemHeightwill always calculate and return the value of the first entry, anditemHeightvalue (initialised fromDefaultItemHeight, and set manually).winforms/src/System.Windows.Forms/src/System/Windows/Forms/ListBox.cs
Lines 605 to 644 in 26ee7b5
As a fix I propose the following:
DefaultValueattribute here, and in other places, where a property value depends on the default font metrics.DefaultItemHeightas obsolete, because it is not a constant.DefaultItemHeightat all call sites with a new private const set at 16, which is the default value in the native ListBox implementation (SYSFONT_CYCHAR 16)weltkante commentedon Jan 28, 2021
Just to be clear, this is already supported and just requires removing the
DefaultValueattribute (as stated) and instead implementingShouldSerializeItemHeightandResetItemHeightmethods (which will be picked up by theTypeDescriptor/PropertyDescriptorinfrastructure used by property grids).RussKie commentedon Jan 28, 2021
Correct.
21 remaining items
Tanya-Solyanik commentedon Mar 2, 2023
@dkazennov - FYI - ShouldSerialize and Reset methods should take into account DrawMode.OwnerDraw*
dkazennov commentedon Mar 7, 2023
@MandiMan hello. Could you please tell me how have you reproduced the issue on the main branch? I don't have extra code for some reason.
@Tanya-Solyanik
ShouldSerializeItemHeight()usesItemHeightproperty. This property checks for DrawMode. I've tried additional checks inShouldSerializeItemHeight(), but it looks like it has the same logic as property itself.MandiMan commentedon Mar 13, 2023
@dkazennov Verified this on VS latest build:17.6.0 Preview 3.0 [33512.13.main]. Please see the following GIF for the reproduction steps.

fix bug The code generated by the ListBox control is not consistent w…
fix bug The code generated by the ListBox control is not consistent w…
fix bug The code generated by the ListBox control is not consistent w…
Eudora-Li01 commentedon Jan 19, 2024
Verified on .NET 9.0.100-alpha.1.24068.28 + latest dlls from Winforms Repo of main branch, it was fixed. The generated code for ListBox1 doesn't have this code: this.listBox1.ItemHeight = 15;
MandiMan commentedon Feb 2, 2024
Verified this issue with .NET 9 Preview 1 test pass build, it was fixed. Test result is same as above.