Powershell/Forms - 如何在选项卡控件中锚定列表视图?
Powershell/Forms - how to anchor listview within a tab control?
我正在尝试将列表视图锚定在表单的标签页中,这样列表视图会随选项卡一起调整大小,并且其他控件也被锚定以允许这样做,但它看起来像是锚定到父表单,而不是选项卡 - 示例代码:
Add-Type -AssemblyName System.Windows.Forms
#
$form = New-Object System.Windows.Forms.Form
$form.MinimumSize = '585,700'
$form.StartPosition = 'CenterScreen'
$form.MaximizeBox = $false
$form.CancelButton = $ExitButton
#Autoscaling settings
$form.AutoScale = $true
$form.AutoScaleMode = "Font"
$ASsize = New-Object System.Drawing.SizeF(7,15)
$form.AutoScaleDimensions = $ASsize
#
#
$MainTab = New-Object System.Windows.Forms.TabControl
$MainTab.Size = '540,465'
$MainTab.Location = '15,95'
$MainTab.Multiline = $True
$MainTab.Name = 'TabPage'
$MainTab.SelectedIndex = 0
$MainTab.Anchor = 'Top,Left,Bottom,Right'
#
$TabPage1 = New-Object System.Windows.Forms.TabPage
$Tabpage1.Name = 'TabPage1'
$Tabpage1.Padding = '5,5,5,5'
$Tabpage1.TabIndex = 1
$Tabpage1.Text = 'Host SSH'
$Tabpage1.UseVisualStyleBackColor = $True
$TabPage1.Enabled = $false
#
$ESXhostList = New-Object System.Windows.Forms.ListView
$ESXhostList.View = [System.Windows.Forms.View]::Details
$ESXhostList.Location = '10,15'
$ESXhostList.Size = '510,150'
$ESXhostList.Columns.Add('Host Name',420) | Out-Null
$ESXhostList.Columns.Add('SSH Status',80) | Out-Null
$ESXhostList.Anchor = 'Top,Left,Right'
#
$ConnectBtn = New-Object System.Windows.Forms.Button
$ConnectBtn.Location = '20,190'
$ConnectBtn.Size = '54,24'
$ConnectBtn.Text = 'Connect'
$ConnectBtn.BackgroundImageLayout = 'Center'
$ConnectBtn.Enabled = $true
$ConnectBtn.Anchor = 'Left,Bottom'
#
$TabPage1.Controls.AddRange(@($ESXhostList,$ConnectBtn))
################################################################################
# TabPage 2
################################################################################
$TabPage2 = New-Object System.Windows.Forms.TabPage
$Tabpage2.Name = 'TabPage2'
$Tabpage2.Padding = '5,5,5,5'
$Tabpage2.TabIndex = 2
$Tabpage2.Text = 'Datastores'
$Tabpage2.UseVisualStyleBackColor = $True
$TabPage2.Enabled = $false
#
$DSList = New-Object System.Windows.Forms.ListView
$DSList.View = [System.Windows.Forms.View]::Details
$DSList.Location ='10,15'
$DSList.Size = '510,150'
$DSList.Columns.Add('Name',160) | Out-Null
$DSList.Columns.Add('FreeGB',65) | Out-Null
#
$ConnectBtn2 = New-Object System.Windows.Forms.Button
$ConnectBtn2.Location = '20,190'
$ConnectBtn2.Size = '54,24'
$ConnectBtn2.Text = 'Connect'
$ConnectBtn2.BackgroundImageLayout = 'Center'
$ConnectBtn2.Enabled = $true
#
$TabPage2.Controls.AddRange(@($DSList,$ConnectBtn2))
#
#
$MainTab.Controls.AddRange(@($TabPage1,$TabPage2))
#
# Info/Logging Window
$ProgressLog = New-Object System.Windows.Forms.TextBox
$ProgressLog.Location = '15,570'
$ProgressLog.Size = '540,80'
$ProgressLog.Multiline = $true
$ProgressLog.Anchor = 'Left,Bottom,Right'
$ProgressLog.TabStop = $false
$ProgressLog.ScrollBars = "Vertical"
$ProgressLog.ReadOnly = $true
#
# Add all the Form controls
$form.Controls.AddRange(@($MainTab,$ProgressLog))
#
#End
# Show form
$form.ShowDialog() | Out-Null
$form.Dispose()
在上面的示例中,选项卡之间的唯一区别是我试图在第一个选项卡中锚定列表视图和按钮控件,但在第二个选项卡中没有锚定。
这是选项卡 1 - 列表视图在选项卡边界之外,而不是锚定在其中,并且按钮完全脱离选项卡区域
这是没有锚定的相同标签 2。正确的布局但没有锚定,因此调整表单大小不会调整列表视图的大小或重新定位按钮:
任何人都可以解释如何做到这一点,因为我读到的所有内容都说锚定选项卡然后锚定其中的控件应该有效吗?
这是我正在处理的内容:
Add-Type -AssemblyName System.Windows.Forms
#
$form = New-Object System.Windows.Forms.Form
$form.MinimumSize = '585,700'
$form.StartPosition = 'CenterScreen'
$form.MaximizeBox = $false
$form.CancelButton = $ExitButton
#Autoscaling settings
$form.AutoScale = $true
$form.AutoScaleMode = "Font"
$ASsize = New-Object System.Drawing.SizeF(7,15)
$form.AutoScaleDimensions = $ASsize
#
#
$MainTab = New-Object System.Windows.Forms.TabControl
$MainTab.Size = '540,465'
$MainTab.Location = '15,95'
$MainTab.Multiline = $True
$MainTab.Name = 'TabPage'
$MainTab.SelectedIndex = 0
$MainTab.Anchor = 'Top,Left,Bottom,Right'
#
$TabPage1 = New-Object System.Windows.Forms.TabPage
$Tabpage1.Name = 'TabPage1'
$Tabpage1.Padding = '5,5,5,5'
$Tabpage1.TabIndex = 1
$Tabpage1.Text = 'Host SSH'
$Tabpage1.UseVisualStyleBackColor = $True
$TabPage1.Enabled = $false
#
$ESXhostList = New-Object System.Windows.Forms.ListView
$ESXhostList.View = [System.Windows.Forms.View]::Details
$ESXhostList.Location = '10,15'
$ESXhostList.Size = '510,150'
$ESXhostList.Columns.Add('Host Name',420) | Out-Null
$ESXhostList.Columns.Add('SSH Status',80) | Out-Null
$ESXhostList.Anchor = 'Top,Left,Right'
#
$ConnectBtn = New-Object System.Windows.Forms.Button
$ConnectBtn.Location = '20,190'
$ConnectBtn.Size = '54,24'
$ConnectBtn.Text = 'Connect'
$ConnectBtn.BackgroundImageLayout = 'Center'
$ConnectBtn.Enabled = $true
$ConnectBtn.Anchor = 'Left,Bottom'
#
$TabPage1.Controls.AddRange(@($ESXhostList,$ConnectBtn))
################################################################################
# TabPage 2
################################################################################
$TabPage2 = New-Object System.Windows.Forms.TabPage
$Tabpage2.Name = 'TabPage2'
$Tabpage2.Padding = '5,5,5,5'
$Tabpage2.TabIndex = 2
$Tabpage2.Text = 'Datastores'
$Tabpage2.UseVisualStyleBackColor = $True
$TabPage2.Enabled = $false
#
$DSList = New-Object System.Windows.Forms.ListView
$DSList.View = [System.Windows.Forms.View]::Details
$DSList.Location ='10,15'
$DSList.AutoSize = [System.Windows.Forms.AutoSizeMode]::GrowAndShrink
$DSList.Columns.Add('Name',160) | Out-Null
$DSList.Columns.Add('FreeGB',65) | Out-Null
$DSList.Anchor = 'Top,Left,Right'
#
$ConnectBtn2 = New-Object System.Windows.Forms.Button
$ConnectBtn2.Location = '10, 120'
$ConnectBtn2.Size = '54,24'
$ConnectBtn2.Text = 'Connect'
#$ConnectBtn2.BackgroundImageLayout = 'Center'
$ConnectBtn2.Enabled = $true
#$ConnectBtn2.Anchor = 'Left,Bottom'
#
$TabPage2.Controls.AddRange(@($DSList,$ConnectBtn2))
#
#
$MainTab.Controls.AddRange(@($TabPage1,$TabPage2))
#
# Info/Logging Window
$ProgressLog = New-Object System.Windows.Forms.TextBox
$ProgressLog.Location = '15,570'
$ProgressLog.Size = '540,80'
$ProgressLog.Multiline = $true
$ProgressLog.Anchor = 'Left,Bottom,Right'
$ProgressLog.TabStop = $false
$ProgressLog.ScrollBars = "Vertical"
$ProgressLog.ReadOnly = $true
#
# Add all the Form controls
$form.Controls.AddRange(@($MainTab,$ProgressLog))
#
#End
# Show form
$form.ShowDialog() | Out-Null
$form.Dispose()
试图找到我用于我创建的 AD 树搜索器表单的博客文章,其中很好地解释了 Winforms 中的对齐、停靠和自动填充问题。可以的话会补充。
好的,我完全通过反复试验设法得到了想要的结果。现在 TabPage1 控件 reposition/resize 但我不得不使用一些非常时髦的位置和大小值:
Add-Type -AssemblyName System.Windows.Forms -ErrorAction Stop
#
$form = New-Object System.Windows.Forms.Form
$form.MinimumSize = '585,700'
$form.StartPosition = 'CenterScreen'
$form.MaximizeBox = $false
$form.CancelButton = $ExitButton
#Autoscaling settings
$form.AutoScale = $true
$form.AutoScaleMode = "Font"
$ASsize = New-Object System.Drawing.SizeF(7,15)
$form.AutoScaleDimensions = $ASsize
#
# Parent Tab Control
$MainTab = New-Object System.Windows.Forms.TabControl
$MainTab.Size = '540,465'
$MainTab.Location = '15,15'
$MainTab.Name = 'TabPage'
$MainTab.SizeMode = 'FillToRight'
$MainTab.Anchor = 'Top,Left,Bottom,Right'
################################################################################
# TabPage 1
################################################################################
$TabPage1 = New-Object System.Windows.Forms.TabPage
$Tabpage1.Padding = '5,5,5,5'
$Tabpage1.Text = 'Host SSH'
$TabPage1.Parent
#
$ESXhostList = New-Object System.Windows.Forms.ListView
$ESXhostList.View = [System.Windows.Forms.View]::Details
$ESXhostList.Location = '10,15'
$ESXhostList.Size = '180,-50'
$ESXhostList.Columns.Add('Host Name',420) | Out-Null
$ESXhostList.Columns.Add('SSH Status',80) | Out-Null
$ESXhostList.Anchor = 'Top,Left,Bottom,Right'
#
$ConnectBtn = New-Object System.Windows.Forms.Button
$ConnectBtn.Location = '10,-20'
$ConnectBtn.Size = '54,24'
$ConnectBtn.Text = 'Connect'
$ConnectBtn.Anchor = 'Left,Bottom'
#
$TabPage1.Controls.AddRange(@($ESXhostList,$ConnectBtn))
################################################################################
# TabPage 2
################################################################################
$TabPage2 = New-Object System.Windows.Forms.TabPage
$Tabpage2.Padding = '5,5,5,5'
$Tabpage2.Text = 'Datastores'
#
$DSList = New-Object System.Windows.Forms.ListView
$DSList.View = [System.Windows.Forms.View]::Details
$DSList.Location ='10,15'
$DSList.Size = '510,150'
$DSList.Columns.Add('Name',160) | Out-Null
$DSList.Columns.Add('FreeGB',80) | Out-Null
# $DSList.Anchor = 'Top,Left,Right'
#
$ConnectBtn2 = New-Object System.Windows.Forms.Button
$ConnectBtn2.Location = '20,190'
$ConnectBtn2.Size = '54,24'
$ConnectBtn2.Text = 'Connect'
#
$TabPage2.Controls.AddRange(@($DSList,$ConnectBtn2))
#
$MainTab.Controls.AddRange(@($TabPage1,$TabPage2))
#
# Info/Logging Window
$ProgressLog = New-Object System.Windows.Forms.TextBox
$ProgressLog.Location = '15,570'
$ProgressLog.Size = '540,80'
$ProgressLog.Multiline = $true
$ProgressLog.Anchor = 'Left,Bottom,Right'
$ProgressLog.TabStop = $false
$ProgressLog.ScrollBars = "Vertical"
$ProgressLog.ReadOnly = $true
#
# Add all the Form controls
$form.Controls.AddRange(@($MainTab,$ProgressLog))
#
#
#End
# Show form
$form.ShowDialog() | Out-Null
$form.Dispose()
但是谁能解释为什么它会这样工作以及发生了什么?
它不起作用的原因与您将控件添加到控件集合的顺序有关。在添加 TabPage 之前,您要添加 ListView 和 Button。 ListView 没有关于 TabPage 宽度的任何信息。 Anchor 参数将导致问题,因为它基于 TabPage.
中的本地坐标
您可以在设置参数后立即添加每个控制对象:
$MainTab.Controls.Add($TabPage1)
或者如果你想保留你的数组并立即添加它们,你必须将你的 $TabPage2
参数移动到 $TabPage1
参数之后,然后在任何之前立即将它们添加在一起其他控件:
$MainTab.Controls.AddRange(@($TabPage1,$TabPage2))
此外,您的 TabControl 需要将本地坐标重置为选项卡的 top-left 角,否则 Anchor 将将您的对象放置在不可见的位置网格上。
这就是您遇到以下问题的原因:
TabPage1 controls now reposition/resize but I had to use some
really funky location and size values:
将以下参数添加到 $MainTab
以解决此问题:
$MainTab.AutoSize = $true
这是一个较旧的问题,但我设法弄明白了。
您只需显式设置 TabPage 本身的大小,仅此而已。
对于默认的 windows 绘图样式,Size 参数如下: System.Drawing.Size(WidthOfParentTabControl - 8, HeightOfParentTabControl - 28)
在 PS 5.1
下测试
编辑:添加了更多控件以更好地展示它
这是完整的代码,只需保存并运行:
using namespace System.Windows.Forms
using namespace System.Drawing
using namespace System.Collections.Generic
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#region Initialize Components
#Mainwindow
$Form = New-Object System.Windows.Forms.Form
$Form.ClientSize = New-Object Point(1280,720)
$Form.text = "Resolution"
$Form.TopMost = $false
#region MainTabControl
[TabControl] $tabControl = New-Object TabControl
$tabControl.Location = New-Object Point(12, 12)
#Size(1248, 668) so I have a 12-12-12-12 "margin" on the tabcontrol
$tabControl.Size = New-Object Size(1256, 696)
$tabControl.Anchor = "top,right,bottom,left"
#endregion
#region Sample Tab
[TabPage] $tabSample = New-Object TabPage
$tabSample.Text = "Sample Tab"
$tabSample.TabIndex = 0
#Size(WParent - 8, HParent - 28)
$tabSample.Size = New-Object Size(1248, 668)
#region Controls
[ListBox] $lbSample = New-Object ListBox
[Button] $bSample = New-Object Button
[Label] $lSample = New-Object Label
$lbSample.Anchor = "left,bottom"
$lbSample.Size = New-Object Size(800, 600)
$lbSample.Location = New-Object Point(12, 50)
$bSample.Anchor = "top,left,bottom,right"
$bSample.Location = New-Object Point(12, 12)
$bSample.Size = New-Object Size("80, 30")
$bSample.Text = "Resizing button"
$lSample.Text = "Anchor (right,bottom)"
$lSample.Anchor = "right,bottom"
$lSample.Location = New-Object Point(1150, 630)
#endregion
$tabSample.Controls.Add($lbSample)
$tabSample.Controls.Add($bSample)
$tabSample.Controls.Add($lSample)
#endregion
#Adding tabs to MainTabControl
$tabControl.Controls.Add($tabSample)
$tabControl.SelectedIndex = 0;
$Form.Controls.Add($tabControl)
#endregion
[void]$Form.ShowDialog()
我正在尝试将列表视图锚定在表单的标签页中,这样列表视图会随选项卡一起调整大小,并且其他控件也被锚定以允许这样做,但它看起来像是锚定到父表单,而不是选项卡 - 示例代码:
Add-Type -AssemblyName System.Windows.Forms
#
$form = New-Object System.Windows.Forms.Form
$form.MinimumSize = '585,700'
$form.StartPosition = 'CenterScreen'
$form.MaximizeBox = $false
$form.CancelButton = $ExitButton
#Autoscaling settings
$form.AutoScale = $true
$form.AutoScaleMode = "Font"
$ASsize = New-Object System.Drawing.SizeF(7,15)
$form.AutoScaleDimensions = $ASsize
#
#
$MainTab = New-Object System.Windows.Forms.TabControl
$MainTab.Size = '540,465'
$MainTab.Location = '15,95'
$MainTab.Multiline = $True
$MainTab.Name = 'TabPage'
$MainTab.SelectedIndex = 0
$MainTab.Anchor = 'Top,Left,Bottom,Right'
#
$TabPage1 = New-Object System.Windows.Forms.TabPage
$Tabpage1.Name = 'TabPage1'
$Tabpage1.Padding = '5,5,5,5'
$Tabpage1.TabIndex = 1
$Tabpage1.Text = 'Host SSH'
$Tabpage1.UseVisualStyleBackColor = $True
$TabPage1.Enabled = $false
#
$ESXhostList = New-Object System.Windows.Forms.ListView
$ESXhostList.View = [System.Windows.Forms.View]::Details
$ESXhostList.Location = '10,15'
$ESXhostList.Size = '510,150'
$ESXhostList.Columns.Add('Host Name',420) | Out-Null
$ESXhostList.Columns.Add('SSH Status',80) | Out-Null
$ESXhostList.Anchor = 'Top,Left,Right'
#
$ConnectBtn = New-Object System.Windows.Forms.Button
$ConnectBtn.Location = '20,190'
$ConnectBtn.Size = '54,24'
$ConnectBtn.Text = 'Connect'
$ConnectBtn.BackgroundImageLayout = 'Center'
$ConnectBtn.Enabled = $true
$ConnectBtn.Anchor = 'Left,Bottom'
#
$TabPage1.Controls.AddRange(@($ESXhostList,$ConnectBtn))
################################################################################
# TabPage 2
################################################################################
$TabPage2 = New-Object System.Windows.Forms.TabPage
$Tabpage2.Name = 'TabPage2'
$Tabpage2.Padding = '5,5,5,5'
$Tabpage2.TabIndex = 2
$Tabpage2.Text = 'Datastores'
$Tabpage2.UseVisualStyleBackColor = $True
$TabPage2.Enabled = $false
#
$DSList = New-Object System.Windows.Forms.ListView
$DSList.View = [System.Windows.Forms.View]::Details
$DSList.Location ='10,15'
$DSList.Size = '510,150'
$DSList.Columns.Add('Name',160) | Out-Null
$DSList.Columns.Add('FreeGB',65) | Out-Null
#
$ConnectBtn2 = New-Object System.Windows.Forms.Button
$ConnectBtn2.Location = '20,190'
$ConnectBtn2.Size = '54,24'
$ConnectBtn2.Text = 'Connect'
$ConnectBtn2.BackgroundImageLayout = 'Center'
$ConnectBtn2.Enabled = $true
#
$TabPage2.Controls.AddRange(@($DSList,$ConnectBtn2))
#
#
$MainTab.Controls.AddRange(@($TabPage1,$TabPage2))
#
# Info/Logging Window
$ProgressLog = New-Object System.Windows.Forms.TextBox
$ProgressLog.Location = '15,570'
$ProgressLog.Size = '540,80'
$ProgressLog.Multiline = $true
$ProgressLog.Anchor = 'Left,Bottom,Right'
$ProgressLog.TabStop = $false
$ProgressLog.ScrollBars = "Vertical"
$ProgressLog.ReadOnly = $true
#
# Add all the Form controls
$form.Controls.AddRange(@($MainTab,$ProgressLog))
#
#End
# Show form
$form.ShowDialog() | Out-Null
$form.Dispose()
在上面的示例中,选项卡之间的唯一区别是我试图在第一个选项卡中锚定列表视图和按钮控件,但在第二个选项卡中没有锚定。
这是选项卡 1 - 列表视图在选项卡边界之外,而不是锚定在其中,并且按钮完全脱离选项卡区域
这是没有锚定的相同标签 2。正确的布局但没有锚定,因此调整表单大小不会调整列表视图的大小或重新定位按钮:
任何人都可以解释如何做到这一点,因为我读到的所有内容都说锚定选项卡然后锚定其中的控件应该有效吗?
这是我正在处理的内容:
Add-Type -AssemblyName System.Windows.Forms
#
$form = New-Object System.Windows.Forms.Form
$form.MinimumSize = '585,700'
$form.StartPosition = 'CenterScreen'
$form.MaximizeBox = $false
$form.CancelButton = $ExitButton
#Autoscaling settings
$form.AutoScale = $true
$form.AutoScaleMode = "Font"
$ASsize = New-Object System.Drawing.SizeF(7,15)
$form.AutoScaleDimensions = $ASsize
#
#
$MainTab = New-Object System.Windows.Forms.TabControl
$MainTab.Size = '540,465'
$MainTab.Location = '15,95'
$MainTab.Multiline = $True
$MainTab.Name = 'TabPage'
$MainTab.SelectedIndex = 0
$MainTab.Anchor = 'Top,Left,Bottom,Right'
#
$TabPage1 = New-Object System.Windows.Forms.TabPage
$Tabpage1.Name = 'TabPage1'
$Tabpage1.Padding = '5,5,5,5'
$Tabpage1.TabIndex = 1
$Tabpage1.Text = 'Host SSH'
$Tabpage1.UseVisualStyleBackColor = $True
$TabPage1.Enabled = $false
#
$ESXhostList = New-Object System.Windows.Forms.ListView
$ESXhostList.View = [System.Windows.Forms.View]::Details
$ESXhostList.Location = '10,15'
$ESXhostList.Size = '510,150'
$ESXhostList.Columns.Add('Host Name',420) | Out-Null
$ESXhostList.Columns.Add('SSH Status',80) | Out-Null
$ESXhostList.Anchor = 'Top,Left,Right'
#
$ConnectBtn = New-Object System.Windows.Forms.Button
$ConnectBtn.Location = '20,190'
$ConnectBtn.Size = '54,24'
$ConnectBtn.Text = 'Connect'
$ConnectBtn.BackgroundImageLayout = 'Center'
$ConnectBtn.Enabled = $true
$ConnectBtn.Anchor = 'Left,Bottom'
#
$TabPage1.Controls.AddRange(@($ESXhostList,$ConnectBtn))
################################################################################
# TabPage 2
################################################################################
$TabPage2 = New-Object System.Windows.Forms.TabPage
$Tabpage2.Name = 'TabPage2'
$Tabpage2.Padding = '5,5,5,5'
$Tabpage2.TabIndex = 2
$Tabpage2.Text = 'Datastores'
$Tabpage2.UseVisualStyleBackColor = $True
$TabPage2.Enabled = $false
#
$DSList = New-Object System.Windows.Forms.ListView
$DSList.View = [System.Windows.Forms.View]::Details
$DSList.Location ='10,15'
$DSList.AutoSize = [System.Windows.Forms.AutoSizeMode]::GrowAndShrink
$DSList.Columns.Add('Name',160) | Out-Null
$DSList.Columns.Add('FreeGB',65) | Out-Null
$DSList.Anchor = 'Top,Left,Right'
#
$ConnectBtn2 = New-Object System.Windows.Forms.Button
$ConnectBtn2.Location = '10, 120'
$ConnectBtn2.Size = '54,24'
$ConnectBtn2.Text = 'Connect'
#$ConnectBtn2.BackgroundImageLayout = 'Center'
$ConnectBtn2.Enabled = $true
#$ConnectBtn2.Anchor = 'Left,Bottom'
#
$TabPage2.Controls.AddRange(@($DSList,$ConnectBtn2))
#
#
$MainTab.Controls.AddRange(@($TabPage1,$TabPage2))
#
# Info/Logging Window
$ProgressLog = New-Object System.Windows.Forms.TextBox
$ProgressLog.Location = '15,570'
$ProgressLog.Size = '540,80'
$ProgressLog.Multiline = $true
$ProgressLog.Anchor = 'Left,Bottom,Right'
$ProgressLog.TabStop = $false
$ProgressLog.ScrollBars = "Vertical"
$ProgressLog.ReadOnly = $true
#
# Add all the Form controls
$form.Controls.AddRange(@($MainTab,$ProgressLog))
#
#End
# Show form
$form.ShowDialog() | Out-Null
$form.Dispose()
试图找到我用于我创建的 AD 树搜索器表单的博客文章,其中很好地解释了 Winforms 中的对齐、停靠和自动填充问题。可以的话会补充。
好的,我完全通过反复试验设法得到了想要的结果。现在 TabPage1 控件 reposition/resize 但我不得不使用一些非常时髦的位置和大小值:
Add-Type -AssemblyName System.Windows.Forms -ErrorAction Stop
#
$form = New-Object System.Windows.Forms.Form
$form.MinimumSize = '585,700'
$form.StartPosition = 'CenterScreen'
$form.MaximizeBox = $false
$form.CancelButton = $ExitButton
#Autoscaling settings
$form.AutoScale = $true
$form.AutoScaleMode = "Font"
$ASsize = New-Object System.Drawing.SizeF(7,15)
$form.AutoScaleDimensions = $ASsize
#
# Parent Tab Control
$MainTab = New-Object System.Windows.Forms.TabControl
$MainTab.Size = '540,465'
$MainTab.Location = '15,15'
$MainTab.Name = 'TabPage'
$MainTab.SizeMode = 'FillToRight'
$MainTab.Anchor = 'Top,Left,Bottom,Right'
################################################################################
# TabPage 1
################################################################################
$TabPage1 = New-Object System.Windows.Forms.TabPage
$Tabpage1.Padding = '5,5,5,5'
$Tabpage1.Text = 'Host SSH'
$TabPage1.Parent
#
$ESXhostList = New-Object System.Windows.Forms.ListView
$ESXhostList.View = [System.Windows.Forms.View]::Details
$ESXhostList.Location = '10,15'
$ESXhostList.Size = '180,-50'
$ESXhostList.Columns.Add('Host Name',420) | Out-Null
$ESXhostList.Columns.Add('SSH Status',80) | Out-Null
$ESXhostList.Anchor = 'Top,Left,Bottom,Right'
#
$ConnectBtn = New-Object System.Windows.Forms.Button
$ConnectBtn.Location = '10,-20'
$ConnectBtn.Size = '54,24'
$ConnectBtn.Text = 'Connect'
$ConnectBtn.Anchor = 'Left,Bottom'
#
$TabPage1.Controls.AddRange(@($ESXhostList,$ConnectBtn))
################################################################################
# TabPage 2
################################################################################
$TabPage2 = New-Object System.Windows.Forms.TabPage
$Tabpage2.Padding = '5,5,5,5'
$Tabpage2.Text = 'Datastores'
#
$DSList = New-Object System.Windows.Forms.ListView
$DSList.View = [System.Windows.Forms.View]::Details
$DSList.Location ='10,15'
$DSList.Size = '510,150'
$DSList.Columns.Add('Name',160) | Out-Null
$DSList.Columns.Add('FreeGB',80) | Out-Null
# $DSList.Anchor = 'Top,Left,Right'
#
$ConnectBtn2 = New-Object System.Windows.Forms.Button
$ConnectBtn2.Location = '20,190'
$ConnectBtn2.Size = '54,24'
$ConnectBtn2.Text = 'Connect'
#
$TabPage2.Controls.AddRange(@($DSList,$ConnectBtn2))
#
$MainTab.Controls.AddRange(@($TabPage1,$TabPage2))
#
# Info/Logging Window
$ProgressLog = New-Object System.Windows.Forms.TextBox
$ProgressLog.Location = '15,570'
$ProgressLog.Size = '540,80'
$ProgressLog.Multiline = $true
$ProgressLog.Anchor = 'Left,Bottom,Right'
$ProgressLog.TabStop = $false
$ProgressLog.ScrollBars = "Vertical"
$ProgressLog.ReadOnly = $true
#
# Add all the Form controls
$form.Controls.AddRange(@($MainTab,$ProgressLog))
#
#
#End
# Show form
$form.ShowDialog() | Out-Null
$form.Dispose()
但是谁能解释为什么它会这样工作以及发生了什么?
它不起作用的原因与您将控件添加到控件集合的顺序有关。在添加 TabPage 之前,您要添加 ListView 和 Button。 ListView 没有关于 TabPage 宽度的任何信息。 Anchor 参数将导致问题,因为它基于 TabPage.
中的本地坐标您可以在设置参数后立即添加每个控制对象:
$MainTab.Controls.Add($TabPage1)
或者如果你想保留你的数组并立即添加它们,你必须将你的 $TabPage2
参数移动到 $TabPage1
参数之后,然后在任何之前立即将它们添加在一起其他控件:
$MainTab.Controls.AddRange(@($TabPage1,$TabPage2))
此外,您的 TabControl 需要将本地坐标重置为选项卡的 top-left 角,否则 Anchor 将将您的对象放置在不可见的位置网格上。
这就是您遇到以下问题的原因:
TabPage1 controls now reposition/resize but I had to use some really funky location and size values:
将以下参数添加到 $MainTab
以解决此问题:
$MainTab.AutoSize = $true
这是一个较旧的问题,但我设法弄明白了。 您只需显式设置 TabPage 本身的大小,仅此而已。 对于默认的 windows 绘图样式,Size 参数如下: System.Drawing.Size(WidthOfParentTabControl - 8, HeightOfParentTabControl - 28)
在 PS 5.1
下测试编辑:添加了更多控件以更好地展示它
这是完整的代码,只需保存并运行:
using namespace System.Windows.Forms
using namespace System.Drawing
using namespace System.Collections.Generic
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#region Initialize Components
#Mainwindow
$Form = New-Object System.Windows.Forms.Form
$Form.ClientSize = New-Object Point(1280,720)
$Form.text = "Resolution"
$Form.TopMost = $false
#region MainTabControl
[TabControl] $tabControl = New-Object TabControl
$tabControl.Location = New-Object Point(12, 12)
#Size(1248, 668) so I have a 12-12-12-12 "margin" on the tabcontrol
$tabControl.Size = New-Object Size(1256, 696)
$tabControl.Anchor = "top,right,bottom,left"
#endregion
#region Sample Tab
[TabPage] $tabSample = New-Object TabPage
$tabSample.Text = "Sample Tab"
$tabSample.TabIndex = 0
#Size(WParent - 8, HParent - 28)
$tabSample.Size = New-Object Size(1248, 668)
#region Controls
[ListBox] $lbSample = New-Object ListBox
[Button] $bSample = New-Object Button
[Label] $lSample = New-Object Label
$lbSample.Anchor = "left,bottom"
$lbSample.Size = New-Object Size(800, 600)
$lbSample.Location = New-Object Point(12, 50)
$bSample.Anchor = "top,left,bottom,right"
$bSample.Location = New-Object Point(12, 12)
$bSample.Size = New-Object Size("80, 30")
$bSample.Text = "Resizing button"
$lSample.Text = "Anchor (right,bottom)"
$lSample.Anchor = "right,bottom"
$lSample.Location = New-Object Point(1150, 630)
#endregion
$tabSample.Controls.Add($lbSample)
$tabSample.Controls.Add($bSample)
$tabSample.Controls.Add($lSample)
#endregion
#Adding tabs to MainTabControl
$tabControl.Controls.Add($tabSample)
$tabControl.SelectedIndex = 0;
$Form.Controls.Add($tabControl)
#endregion
[void]$Form.ShowDialog()