Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Winforms Powershell/Forms-如何在选项卡控件中锚定listview?_Winforms_Powershell_Tabs_Anchorpoint - Fatal编程技术网

Winforms Powershell/Forms-如何在选项卡控件中锚定listview?

Winforms Powershell/Forms-如何在选项卡控件中锚定listview?,winforms,powershell,tabs,anchorpoint,Winforms,Powershell,Tabs,Anchorpoint,我试图在表单中的选项卡页面中锚定listview,这样listview会随着选项卡调整大小,其他控件也会锚定以允许此操作,但看起来它锚定到父表单,而不是选项卡示例代码: Add-Type -AssemblyName System.Windows.Forms # $form = New-Object System.Windows.Forms.Form $form.MinimumSize = '585,700' $form.StartPosition = 'CenterScreen' $form.M

我试图在表单中的选项卡页面中锚定listview,这样listview会随着选项卡调整大小,其他控件也会锚定以允许此操作,但看起来它锚定到父表单,而不是选项卡示例代码:

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()
在上面的示例中,两个选项卡之间的唯一区别是,我试图在第一个选项卡中锚定listview和button控件,但在第二个选项卡中没有锚定

这里是选项卡1-listview位于选项卡边界之外,而不是锚定在选项卡内,并且按钮完全脱离选项卡区域

这是相同的选项卡2,没有锚定。布局正确但没有锚定,因此调整窗体大小不会调整listview的大小或重新定位按钮:


有谁能解释一下如何做到这一点,因为我读到的每一篇文章都说锚定选项卡然后锚定其中的控件应该会起作用?

以下是我的工作:

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()

试图找到我创建的一个广告树搜索器表单的博客文章,它很好地解释了Winforms中的对齐、停靠和自动填充问题。如果可以的话,我会添加。

好的,我完全是通过反复试验获得了想要的结果。TabPage1控件现在重新定位/调整大小B但我不得不使用一些非常时髦的位置和大小值:

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()

但是有人能解释它为什么会这样工作以及发生了什么吗?

它不工作的原因与向控件集合添加控件的顺序有关。在添加选项卡页之前,您正在添加ListView和按钮。然后,ListView没有关于选项卡页面宽度的任何信息。锚点参数将导致问题,因为它基于选项卡页面中的局部坐标

您可以在设置参数后立即添加每个控件对象:

$MainTab.Controls.Add($TabPage1)
或者,如果您想保留阵列并立即添加它们,则必须将
$TabPage2
参数移动到
$TabPage1
参数之后,然后立即将它们添加到任何其他控件之前:

$MainTab.Controls.AddRange(@($TabPage1,$TabPage2))
此外,TabControl需要将局部坐标重置为选项卡的左上角,否则锚点将在不可见位置栅格上定位对象

这就是您出现以下问题的原因:

TabPage1控件现在可以重新定位/调整大小,但我不得不使用一些 非常时髦的位置和大小值:

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()
将以下参数添加到
$MainTab
以解决此问题:

$MainTab.AutoSize = $true

第二个选项卡或控件上没有锚定语句,并且第二个选项卡的Name属性与第一个选项卡相同。将锚语句添加到第二个选项卡并更正名称,使第二个选项卡与我的第一个选项卡类似。他们看起来仍然不一定是对的,但这是按照你要求的方式进行的我没有在第二个选项卡上放置锚来说明添加锚和不添加锚的区别。这个名字与锚定无关,但我已经修改了拼写错误。在illustrateSorry dude中添加了图像,我误解了你的问题:$DSList.AutoSize=[System.Windows.Forms.AutoSizeMode]::growthandshrink没有;调整父窗体的大小时无法工作。再次澄清。我希望将listview锚定到选项卡,以便在调整主窗口的大小时,它能够正确地随选项卡调整大小,而不是超出选项卡的边界。自动调整大小也不能解决按钮位置不正确的问题。我的帖子不清楚——我不得不去参加一个会议。listview超出tab控件边界的原因是,它的静态大小比您分配给表单的宽度还要大。完全删除ListView2项上的.Size定义,并使用我提供的自动调整大小代码。当我这样做(delete.Size和use.AutoSize)时,listview2会随着主窗体的增长和收缩而扩展到tab2。感谢您发布代码,但是是的,您误解了我的意图。我想将listview锚定到底部,这样它就可以随着窗体大小的调整而向下扩展(就像锚定到顶部/左侧/底部/右侧以及固定的开始大小一样),因此我需要将按钮锚定到“底部”以按比例移动它-选项卡上还有其他控件需要锚定(我简化了表单,将其发布在这里)。很抱歉出现任何误解,选项卡页有本地坐标。设置锚参数将在(不可见)上移动x0,y0位置9平铺锚定网格。如果设置
anchor='Bottom,Right'
x0,y0将位于右下平铺的左上角。这就是为什么必须使用
-
值向后移动。您需要在初始TabControl中设置
AutoSize=$True
,以将坐标重置为始终位于您的左上角r标签页。