C#的WinForms垂直对齐的文本框,等文本框、WinForms

2023-09-11 07:35:09 作者:旧城旧梦旧忍人心

我工作的一个项目,更新他们的WinForms应用程序UI与尺寸更一致。文本框和ComboBox控件具有不同的高度通过默认,即使是相同的字体。我已经能够通过关闭自动调整大小来调整文本框,但文本还是拥抱控制的顶部,留下一个缺口下方。

有没有什么办法可以在控制垂直居中的文字?

解决方案

如果您关闭自动调整大小上的控件,它必须是一个标签,因为文本框不具有自动调整大小属性。一个标签的TextAlign属性的类型ContentAligment的,所以你可以设置水平和垂直对齐方式。

有关各种无聊的原因,在Windows文本框是用来自动调整高度,以使用的字体。要控制高度和垂直居中的文字,您可以快速创建一个自定义的用户控件,您可以用替换所有的文本框。

如何使用C TextBox控件

在用户控件,设置边框为Fixed3D和背景色,以System.Window。添加一个文本框,并设置其边框为无。在Resize事件进行控制,加code,使文本框的宽度相同的用户控件的客户区(占边界像素)和左对齐它(即 textBox1.Left = 0; )和垂直居中它(例如: textBox1.Top =(this.Height - textBox1.Height)/ 2;

最后,添加到用户的控制,你需要(可能只是文字和框TextChanged,我猜),并且将它们连接起来,使他们通过对文本框通过您的控件中,像这样的任何文本框类型的属性和事件:

 公共字符串文本
{
    得到
    {
        返回textBox1.Text;
    }
    组
    {
        textBox1.Text =价值;
    }
}
 

如果你想获得超看中这一点,你甚至可以用一个实际的类型的ContentAlignment(如标签)取代你的用户控件的TextAlign属性,然后对准内部文本框相匹配。

这同样的方法将适用于一个组合框,虽然它看起来有点奇怪。随着组合框,设置它的FlatStyle属性为平 - 否则,你处理它同一个文本框。它会看起来很奇怪,因为下拉箭头框将不会那么在面板的顶部和底部。

I'm working on a project updating their WinForms application UI to be more consistent with sizes. TextBox and ComboBox controls have different heights by default, even with the same font. I've been able to resize the text boxes by turning off AutoSize, but the text still hugs the top of the control, leaving a gap below.

Is there any way to center the text vertically in the control?

解决方案

If you're turning off AutoSize on a control, it must be a Label, since TextBox doesn't have an AutoSize property. The TextAlign property of a Label is of type ContentAligment, so you can set both horizontal and vertical alignment.

For various boring reasons, TextBoxes in windows are intended to auto-adjust their heights to the font used. To control the heights and vertically center the text, you can quickly create a custom UserControl that you can replace all your textboxes with.

On your user control, set the BorderStyle to Fixed3D and the BackColor to System.Window. Add a TextBox and set its BorderStyle to None. In the Resize event for the control, add code that makes the TextBox the same width as the user control's client area (accounting for the border pixels) and left-aligns it (i.e. textBox1.Left = 0;) and vertically centers it (e.g. textBox1.Top = (this.Height - textBox1.Height) / 2;).

Finally, add to the user control any TextBox-type properties and events you need (probably just Text and TextChanged, I would guess), and wire them up so that they pass through to the TextBox inside your control, like this:

public string Text
{
    get
    {
        return textBox1.Text;
    }
    set
    {
        textBox1.Text = value;
    }
}

If you wanted to get super-fancy with this, you could even replace your user control's TextAlign property with one that is actually of type ContentAlignment (like the Label) and then align the inner TextBox to match.

This same approach will work for a ComboBox, although it will look slightly odd. With the ComboBox, you set its FlatStyle property to Flat - otherwise you deal with it the same as a TextBox. It will look odd because the dropdown arrow box won't be quite at the top and bottom of the panel.