【发布时间】:2016-11-02 01:11:37
【问题描述】:
在 WPF 中,它默认支持 TextBox 控件的垂直对齐方式。但在 Windows 窗体中,无法设置 TextBox 控件的垂直对齐方式。
在我的例子中,我使用了多行文本框,文本需要显示在文本框的底部,并且在输入时需要保持对齐。
我尝试获取输入文本的行数,并尝试根据文本长度计算文本框的边界。但是在使用自动换行编辑文本时,行数不正确。
谁能帮助我在编辑时保持 TextBox 的垂直对齐?
我尝试使用该线程中给出的建议更改文本框的位置。编辑文本时。当我尝试编辑文本时,边界没有正确更新,并且文本的某些部分隐藏在文本框中。我已经根据字体大小和文本宽度计算了边界。
Size textSize = TextRenderer.MeasureText(TextBoxText, this.textBoxControl.Font);
int textBoxTop = this.textBoxControl.Bounds.Top;
int nol = (textSize.Width > this.textBoxControl.Width) ? ((textSize.Width) / this.textBoxControl.Width) + 1 : 1;
{
if (nol > n)
{
n = nol;
rect1 = this.textBoxControl.Bounds;
if (top + (height - nol * textSize.Height) > top)
{
rect1.Y = top + (height - nol * textSize.Height);
rect1.Height = nol * textSize.Height;
this.textBoxControl.Bounds = rect1;
}
else
{
this.textBoxControl.Bounds = rect1;
}
}
else if (nol < n)
{
n = nol;
rect1 = this.textBoxControl.Bounds;
if (rect1.Y + nol * textSize.Height < top + height)
{
rect1.Y += textSize.Height - this.textBoxControl.Margin.Top;
rect1.Height -= textSize.Height;
//this.textBoxControl.Bounds = rect1;
}
if (nol == 1)
{
rect1.Y = top + height - textSize.Height;
rect1.Height = textSize.Height;
//this.textBoxControl.Bounds = rect1;
}
this.textBoxControl.Bounds = rect1;
}
}
它在编辑文本时工作正常,但在某些情况下,nol 行数计算错误。如何获取包含换行的文本框的实际行数。?
【问题讨论】:
-
我尝试使用该线程中给出的建议更改文本框的位置。编辑文本时。
-
您可以在 WinForms 中从 WPF 托管 TextBox。
-
使用 .Top 进行某种形式的计算以改变垂直对齐方式,例如textBox1.Top = (this.Height - textBox1.Height) / 2;) 中心对齐或使用标签怎么样?它具有垂直对齐并且没有文本框的重新对齐垃圾。
标签: c# winforms vertical-alignment