我在powershell中尝试GUI。我正在摆弄TableLayoutPanel
,以了解如何使用它。特别是我试图设置列的宽度。我不明白的是为什么ColumnStyles
集合是空的。
我理解应该有一个条目集合中的每一列但Count
属性总是0。我做错了什么?
谢谢你的帮助。
Add-Type -assembly System.Windows.Forms
$main_form = New-Object System.Windows.Forms.Form
$main_form.Text ='SMTP Tester'
$main_form.Width = 600
$main_form.Height = 400
$layout = New-Object System.Windows.Forms.TableLayoutPanel
$layout.ColumnCount = 2
$layout.RowCount = 10
$layout.Dock = [System.Windows.Forms.DockStyle]::Fill
$main_form.Controls.Add($layout)
$smtpserver_label = New-Object System.Windows.Forms.Label
$smtpserver_label.Text = "SMTP Server"
$smtpserver_label.TextAlign = [System.Drawing.ContentAlignment]::MiddleLeft
$layout.Controls.Add($smtpserver_label,0,0)
$smtpserver_textbox = New-Object System.Windows.Forms.TextBox
$layout.Controls.Add($smtpserver_textbox,1,0)
$port_label = New-Object System.Windows.Forms.Label
$port_label.Text = "Port"
$port_label.TextAlign = [System.Drawing.ContentAlignment]::MiddleLeft
$layout.Controls.Add($port_label,0,1)
$port_textbox = New-Object System.Windows.Forms.TextBox
$port_textbox.Text = "25"
$layout.Controls.Add($port_textbox,1,1)
$usetls_checkbox = New-Object System.Windows.Forms.CheckBox
$usetls_checkbox.Text = "Use TLS"
$layout.Controls.Add($usetls_checkbox,0,2)
$layout.ColumnStyles.Count
$layout.ColumnStyles.Item(0).SizeType = [System.Windows.Forms.SizeType]::Absolute
$layout.ColumnStyles.Item(0).Width = 100
$layout.ColumnStyles.Item(1).SizeType = [System.Windows.Forms.SizeType]::AutoSize
$main_form.ShowDialog()
正如@Jimi在评论中指出的,您需要手动创建ColumnStyle对象并将它们添加到ColumnStyles集合中。$layout.ColumnCount
属性不像我想的那样自动分配那些对象。
$columnstyle = New-Object System.Windows.Forms.ColumnStyle
$res = $layout.ColumnStyles.Add($columnstyle)
$columnstyle = New-Object System.Windows.Forms.ColumnStyle
$res = $layout.ColumnStyles.Add($columnstyle)
$layout.ColumnStyles.Item(0).SizeType = [System.Windows.Forms.SizeType]::Absolute
$layout.ColumnStyles.Item(0).Width = 100
$layout.ColumnStyles.Item(1).SizeType = [System.Windows.Forms.SizeType]::AutoSize