A TextBox
in .NET does not let you adjust its height (there isn't even an AutoSize
option).
i have a textbox that is cutting off the bottom of text in the box:
Example 1:
Example 2:
What i need is to fix the PreferredSize
calculation; to override the bug in .NET WinForms TextBox
control.
i created a descendant FixedTextBox
, and tried overriding the protected GetPreferredSize
:
public override Size GetPreferredSize(Size proposedSize)
{
Size size = base.GetPreferredSize(proposedSize);
if (this.BorderStyle == BorderStyle.None)
{
size.Height += 2;
}
return size;
}
My overridden method is being called, but the TextBox
isn't altering it's "preferred" size.
i also tried overriding the protected DefaultSize
property:
protected override Size DefaultSize
{
get
{
Size size = base.DefaultSize;
if (this.BorderStyle == BorderStyle.None)
{
size.Height += 2;
}
return size;
}
}
And it is called during construction, but is never called again when the "default size" is different (e.g. after i change the BorderStyle
), and doesn't affect the size of the TextBox.
What is the proper way to hook into the .NET WinForms "AutoSize" infrastructure, to adjust the "preferred" size?
Note: Just because i overrode
GetPreferredSize
doesn't mean the solution involves overridingGetPreferredSize
tl;dr: Someone step into textBox1.Height += 1
and figure why it does nothing.
Related (but different) questions
Changing WinForms TextBox to BorderStyle.None causes text to be cut off
Mentions the problem, accepting solutions; contrast with this question which asks how to use the "preferred size" infrastructure in WinForms
How to make TextBox rethink it's preferred height?
Mentions the problem, looking for solutions involving having the TextBox recalculate its preferred height; contrast to this question which accepts that the textbox cannot recalculate its preferred height, and even if it could it wouldn't help because the auto-size calculation is wrong