65.9K
CodeProject 正在变化。 阅读更多。
Home

设置 TextBox 高度

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.92/5 (6投票s)

2008年9月7日

CPOL

2分钟阅读

viewsIcon

157670

downloadIcon

3742

如何更改单行文本框的高度

引言

本文介绍如何设置单行 textbox 控件的高度。 C# (和 VB.NET) 中的 TextBox 控件会忽略 height 属性,并调整其高度以适应字体高度。 通过计算所需的精确字体大小,我们可以将 TextBox 设置为所需的高度。

背景

我有一个 C# 项目,需要一个具有可调节高度的单行 textbox。 我找到了许多关于如何调整宽度的示例,但没有关于如何动态更改 textbox 高度的示例。 我不想使用多行,因为我想保留单行 textbox 的自动完成功能。

Using the Code

单行 textbox 高度由字体大小设置,而不是由 TextBox.Height 属性设置。 如果您正在寻找精确的高度,这会使其变得困难。 幸运的是,字体属性使用 float 作为字体大小 (emSize)。 您可以使用字体的小数来微调 textbox 的高度。

textbox 用于确定其高度的计算是

Height = ( Font Size * Font Line Spacing / Font Em Height ) + 7
  • 字体大小 - 以像素为单位测量字体大小最简单,因此您不必考虑屏幕 dpi。
  • 字体行间距 - 两条连续文本行之间的距离(以设计单位为单位)。
  • 字体 Em 高度 - 字体最宽字母(通常为字母 M)的设计单位高度。

Textboxes 在字体高度周围有 3 像素的下部和 4 像素的上部空白。 因此,计算会将高度增加 7 像素。

我们可以反转此计算以获得所需高度的字体大小

Font Size = ( height - 7 ) * Font Em Height / Font Line Spacing

此方法将返回一个 font 对象,该对象将设置您的 textbox 的大小

private Font GetFontForTextBoxHeight(int TextBoxHeight, Font OriginalFont)
{
    // What is the target size of the textbox?
    float desiredheight = (float)TextBoxHeight;

    // Set the font from the existing TextBox font.
    // We use the fnt = new Font(...) method so we can ensure that
    //  we're setting the GraphicsUnit to Pixels.  This avoids all
    //  the DPI conversions between point & pixel.
    Font fnt = new Font(OriginalFont.FontFamily,
                        OriginalFont.Size,
                        OriginalFont.Style,
                        GraphicsUnit.Pixel);

    // TextBoxes never size below 8 pixels. This consists of the
    // 4 pixels above & 3 below of whitespace, and 1 pixel line of
    // greeked text.
    if (desiredheight < 8)
        desiredheight = 8;

    // Determine the Em sizes of the font and font line spacing
    // These values are constant for each font at the given font style.
    // and screen DPI.
    float FontEmSize = fnt.FontFamily.GetEmHeight(fnt.Style);
    float FontLineSpacing = fnt.FontFamily.GetLineSpacing(fnt.Style);

    // emSize is the target font size.  TextBoxes have a total of
    // 7 pixels above and below the FontHeight of the font.
    float emSize = (desiredheight - 7) * FontEmSize / FontLineSpacing;
    
    // Create the font, with the proper size.
    fnt = new Font(fnt.FontFamily, emSize, fnt.Style, GraphicsUnit.Pixel);

    return fnt;
}

每当您必须设置 textbox 大小时,请使用上述方法设置 font 属性

YourTextBox.Font = GetFontForTextBoxHeight(DesiredHeight, YourTextBox.Font);  

使用控件

可调节 Height Textbox 是一个您可以添加到表单中的用户控件。 它的行为与常规 TextBox 相同,除了

  • 您可以通过 Size_AdjustableHeight 属性在设计视图中设置高度。
  • 该控件(如果在单行模式下)将响应停靠和锚定。

如何使用该控件

  1. 在设计视图中右键单击您的工具箱。
  2. 单击浏览。 导航到 *AdjustableHeightTextbox.dll*。 点击确定。
  3. 将控件添加到您的表单。

玩得开心!

关注点

  • 我了解到 .NET TextBox 代码基本上是旧 MFC textbox 控件的包装器。 这就是它不是很可定制的原因。
  • GetFontForTextBoxHeight() 例程经过一些调整后,可以用于 ComboBox 控件。 高度计算很接近,但仍然相差几个像素。

缺乏关于 textbox 控件内部工作原理的信息(与大多数标准 .NET 控件一样)。 我希望本文能为您节省数小时的搜索时间。

历史

  • 2008.09.07 - 发布原始文章
  • 2008.09.10 - 添加了 textbox 的用户控件版本和一个测试项目
© . All rights reserved.