.Bottom是只读的

时间:2017-03-10 14:35:22

标签: winforms powershell listbox resize

我正在使用PowerShell脚本,该脚本使用停靠的表单和面板。我能够很好地创建表单和面板,但是我在使用$inputbox.Bottom = $form.Height - 215来控制大小时在运行时调整列表框时遇到问题,但我收到了错误

  

'Bottom'是一个只读属性。

只需在面板中使用Fill就行了,因为我在列表框的上方和下方都有按钮。以下是我的代码示例:

[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)

$form.ResizeEnd
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(1040,459)
$form.KeyPreview = $true
$form.StartPosition = ‘centerscreen’
$form.BackColor = 'MidnightBlue'
$form.Add_KeyDown({if($_.KeyCode -eq "Escape"){$form.Close()}})
$form.Text = "Dialog Box 2.0" 
$form.Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell_ise.exe")

$buttonPanel3 = New-Object Windows.Forms.Panel
$buttonPanel3.Size = New-Object Drawing.Size @(290,70)
$buttonPanel3.Dock = "left"
$buttonPanel3.BackColor = 'Blue'

$inputbox = New-Object System.Windows.Forms.ListBox
$inputbox.BorderStyle = 'NONE'
$inputbox.Font = New-Object System.Drawing.Font(“segoe UI”,9)
$inputbox.SelectionMode = "MultiExtended"
$inputbox.Left = 10
$inputbox.Top = 105
$inputbox.Width = 200
$inputbox.Bottom = $form.Height -215
$inputbox.Height = $form.Height -215

$buttonPanel3.Controls.Add($inputbox)
$form.Controls.Add($buttonPanel3)
$form.ShowDialog()

如果有人可以提供一些列表框的示例代码,当您调整表格时,该列表框会调整大小(主要与垂直扩展有关)。

1 个答案:

答案 0 :(得分:1)

我最终完全重新整理了我的表单并使用了填充方法,但是我想对表单大小调整进行一些修改,因为这是真正的问题。

表单的resize处理程序为$form.Add_Resize({})。在括号内,我可以根据需要修改高度和宽度属性。在这种情况下,我只想要height属性,所以代码就像:

[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)

$form.ResizeEnd
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(1040,459)
$form.KeyPreview = $true
$form.StartPosition = ‘centerscreen’
$form.BackColor = 'MidnightBlue'
$form.Add_KeyDown({if($_.KeyCode -eq "Escape"){$form.Close()}})
$form.Text = "Dialog Box 2.0" 
$form.Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell_ise.exe")
$form.Add_Resize({
    $inputbox.Height = $form.Height -215
})

$buttonPanel3 = New-Object Windows.Forms.Panel
$buttonPanel3.Size = New-Object Drawing.Size @(290,70)
$buttonPanel3.Dock = "left"
$buttonPanel3.BackColor = 'Blue'

$inputbox = New-Object System.Windows.Forms.ListBox
$inputbox.BorderStyle = 'NONE'
$inputbox.Font = New-Object System.Drawing.Font(“segoe UI”,9)
$inputbox.SelectionMode = "MultiExtended"
$inputbox.Left = 10
$inputbox.Top = 105
$inputbox.Width = 200
$inputbox.Height = $form.Height -215

$buttonPanel3.Controls.Add($inputbox)
$form.Controls.Add($buttonPanel3)
$form.ShowDialog()