Add-Type -AssemblyName System.Windows.Forms
# Create a sample form.
$form = [Windows.Forms.Form] @{
Text = 'Form with Text Boxes'
ClientSize = [Drawing.Point]::new(500, 110)
}
# Create the controls and add them to the form.
$form.Controls.AddRange(@(
[Windows.Forms.Label] @{
Text = 'Foo:'
Location = [Drawing.Point]::new(10, 10)
AutoSize = $true
}
# Single-line text box.
[Windows.Forms.TextBox] @{
Text = 'Single-line'
Location = [Drawing.Point]::new(50, 10)
# Set the width.
Width = 200
# Note: For *single-line* text boxes, the height is *automatically and invariably set*,
# namely to the height of *one line of text* based on the text box' font (plus margins).
# Any attempt to set a height manually - whether via .Height or via .Size -
# is *quietly ignored*.
# It follows from the above that you can control the height *indirectly*, by changing
# the *font size*.
Font = [Drawing.Font]::new($form.Font.Name, 24)
}
# Multiline text box.
[Windows.Forms.TextBox] @{
Multiline = $true
Text = "Multi-`r`nline" # Note: use "`r`n" (CRLF), not just "`n"
Location = [Drawing.Point]::new(280, 10)
# Multiline text boxes DO allow setting a height too.
Size = [Drawing.Size]::new(200, 90)
}
))
$null = $form.ShowDialog()
1条答案
按热度按时间rbpvctlc1#
Jimi提供关键指针的提示:
使用WinForms时:
TextBox
示例,其.Multiline
属性设置为$false
(默认值)),height * 会自动且不变地设置,即设置为 * 一行文本的高度 *(加上页边距),基于文本框的字体,默认情况下,继承自它们的(直接)父控件(包含窗体或嵌套在其中的容器控件)。.Height
还是通过.Size
--都会被悄悄忽略。下面是自包含的示例代码,它显示了如何通过增加字体大小来增加单行文本框的高度,以及如何通过
.Size
(或.Height
)控制多行文本框的高度:运行此代码将显示以下表单:
如果注解掉上面的
Font = ...
行,则单行文本框将使用与其他控件相同的字体,因此将按比例呈现。