如何覆盖 WinForms TextBox 的首选大小? [英] How to override the preferred size of a WinForms TextBox?
问题描述
.NET 中的 TextBox
不允许您调整其高度(有 计算;覆盖 .NET WinForms TextBox
控件中的错误.
我创建了一个后代 FixedTextBox
,并尝试覆盖受保护的 GetPreferredSize
:
public override Size GetPreferredSize(Size proposalSize){大小大小 = base.GetPreferredSize(proposedSize);如果(this.BorderStyle == BorderStyle.None){尺寸.高度 += 2;}退货尺寸;}
我的重写方法正在被调用,但TextBox
并没有改变它的首选"大小.
我还尝试覆盖受保护的 DefaultSize
属性:
protected override Size DefaultSize{得到{尺寸 size = base.DefaultSize;如果(this.BorderStyle == BorderStyle.None){尺寸.高度 += 2;}退货尺寸;}}
它在构造过程中被调用,但当默认大小"不同时(例如,在我更改 BorderStyle
之后)不再被调用,并且不会影响 TextBox 的大小.
挂钩到 .NET WinFormsAutoSize"基础结构以调整首选"大小的正确方法是什么?
<块引用>注意:仅仅因为我覆盖了 GetPreferredSize
并不意味着解决方案涉及覆盖 GetPreferredSize
tl;dr:有人进入 textBox1.Height += 1
并弄清楚为什么它什么都不做.
相关(但不同)的问题
更改 WinFormsTextBox to BorderStyle.None 导致文本被截断
提及问题,接受解决方案;与询问如何在 WinForms 中使用首选大小"基础结构的问题形成对比
提到问题,寻找解决方案,包括让 TextBox 重新计算其首选高度;与这个接受文本框无法重新计算其首选高度的问题相反,即使可以它也无济于事,因为自动大小计算是错误的
请参阅 Phil Wright 对 SO 问题的回答 Control.GetPreferredSize 方法的目的是什么?:
作为布局周期的一部分,容器会调用 Control.GetPreferredSize.
如果可能,它允许被调用的控件返回他们想要的大小.然而,容器不必遵守这个请求的大小.例如,当控件的 Dock 设置为 Top 时,无论从 GetPreferredSize 方法返回的值如何,宽度都将定义为包含控件的宽度.这种方法对于像流布局控件这样的容器特别有用,它会一个接一个地定位每个子控件."[Phil Wright]
这意味着这个 PreferredSize 不会改变你的 TextBox 的大小.
<小时>我认为这可以解决您的问题:
公共类 MyTextBox : TextBox{const int RequestedHight = 30;protected override void OnSizeChanged(EventArgs e){base.OnSizeChanged(e);AssureRequestedHight();}受保护的覆盖 void OnCreateControl(){base.OnCreateControl();AssureRequestedHight();}私有无效 AssureRequestedHight(){if (this.Size.Height != RequestedHight && !this.Multiline) {this.Multiline = true;this.MinimumSize = new Size(0, RequestedHight);this.Size = new Size(this.Size.Width, RequestedHight);this.Multiline = false;}}}
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
See the answer of Phil Wright for the SO question What is the purpose of Control.GetPreferredSize method?:
"The Control.GetPreferredSize is called by containers as part of the layout cycle.
It allows the called control to return the size they would like to have if possible. The container does not have to honor this requested size however. For example, when a control has a Dock setting of Top the width would be defined as the width of the containing control regardless of the value returned from the GetPreferredSize method. This method is particularly useful for containers like the flow layout control which will position each child control one after another." [Phil Wright]
This means that this PreferredSize will not change the size of your TextBox.
I think this solves your problem:
public class MyTextBox : TextBox
{
const int RequestedHight = 30;
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
AssureRequestedHight();
}
protected override void OnCreateControl()
{
base.OnCreateControl();
AssureRequestedHight();
}
private void AssureRequestedHight()
{
if (this.Size.Height != RequestedHight && !this.Multiline) {
this.Multiline = true;
this.MinimumSize = new Size(0, RequestedHight);
this.Size = new Size(this.Size.Width, RequestedHight);
this.Multiline = false;
}
}
}
这篇关于如何覆盖 WinForms TextBox 的首选大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!