我有一个tablelayoutpanel,我打开了自动滚动功能。我想完全隐藏水平滚动条,但看不出这是怎么回事。看起来我可以将VScroll设置为true并将HScroll设置为false但我不确定如何从tablelayoutpanel对象访问这些属性。如何隐藏水平滚动条?
[CmdletBinding()]
Param()
Set-StrictMode -Version 2
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
Write-Verbose "Create the main form"
$form = New-Object Windows.Forms.Form
$form.Size = New-Object Drawing.Size @(900, 600)
$form.SuspendLayout()
$dataSize = 70
$dataHeight = 20
$volumeSize = 280
$dp = New-Object Windows.Forms.TableLayoutPanel
$addButton = New-Object Windows.Forms.Button
$addButton.Text = "Add"
$addButton.name = "outputButton"
$addButton.Add_Click({Add-Row})
$addButton.Location = New-Object System.Drawing.Point(0,500)
$correctedWidth = 0
Function Add-Row ($txt) {
write-verbose "add row"
$script:dp.RowCount++
$zeroBasedRow = $dp.RowCount - 1
write-verbose $zeroBasedRow
for ($i=0; $i -lt 7; $i++) {
$label = New-Object System.Windows.Forms.Label
$label.TextAlign = "MiddleCenter"
$label.Text = "0"
$label.Font = "Verdana, 9pt"
$label.Size = "$dataSize,$dataHeight"
$label.BackColor = "Transparent"
if ($i -eq 0) {
$label.Size = "$correctedWidth,$dataHeight"
if ($txt -eq $null){
$label.Text = "12345678901234567890123456789012"
} else {
$label.Text = $txt
}
}
$dp.Controls.Add($label,$i,$zeroBasedRow)
}
}
Function Create-Data-Panel ($width, $height, $correction) {
Write-Verbose "Create the panel that holds the data"
$dataPanel = New-Object Windows.Forms.TableLayoutPanel
$dataPanel.Size = New-Object Drawing.Size @($width,$height)
$dataPanel.AutoScroll = $true
#$dataPanel.VerticalScroll.Enabled = $true
#$dataPanel.VerticalScroll.Visible = $true
#$dataPanel.HorizontalScroll.Enabled = $false
#$dataPanel.HorizontalScroll.Visible = $false
$dataPanel.BackColor = "Transparent"
$dataPanel.CellBorderStyle = "None"
$dataPanel.RowCount = 0
$dataPanel.ColumnCount = 7
$script:correctedWidth = $volumeSize - $correction
return $dataPanel
}
$hght = $form.size.height * 3/4
$dp = Create-Data-Panel 760 300 4
for ($j=0; $j -lt 50; $j++){ # LINE OF INTEREST!!!!!!
$txt = "$j 12345678901234567890123456789012"
Add-Row $txt
}
$form.Controls.Add($dp)
$form.Controls.Add($addButton)
$form.Add_Shown( { $form.Activate() } )
$form.ResumeLayout()
$form.StartPosition = "CenterScreen"
$form.ShowDialog()
请注意,AutoScroll已启用。如果您按原样运行此脚本,则TableLayoutPanel将仅显示垂直滚动条;推动"添加"根据需要多次按钮,您将看到表格末尾有新条目,滚动条仅显示垂直滚动条。问题是我正在使用的TableLayoutPanel上没有任何初始数据,因此基本上是" LINE OF INTEREST !!!!!!"对于$ j边界值变为0。将该值更改为零,以便该行读取" for($ j = 0; $ j -lt 0; $ j ++){#LINE OF INTEREST !!!!!!"然后运行脚本(注意表是空的。按"添加"按钮(注意没有滚动条显示,直到需要它们这不是问题)。填充表格以便垂直滚动条是必要的,你会看到两个垂直和水平滚动条出现 - 我只想垂直滚动条不是水平 - 所以我的问题是如何隐藏水平滚动条?谢谢!
答案 0 :(得分:0)
问题是滚动条在面板中向内向内延伸,减少了嵌入式控件的可用空间。
因此,一旦由于.AutoScroll
设置为$true
而显示垂直滚动条,标签就不再有足够的水平空间,而且水平滚动条也会显示。
将.AutoScroll
设置为$true
后,您无法使用.HorizontalScrollBar.Visble = $false
等语句手动关闭滚动条 - 此类尝试将被忽略。
我确信有更好的解决方案,但这是一个快速修复,即:
在显示后者时,按垂直滚动条的宽度加宽面板,这会导致.AutoScroll
最终不显示水平滚动条,
但请注意,当隐藏导致垂直滚动条显示的行时,水平滚动条会隐藏
Function Add-Row ($txt) {
write-verbose "Adding row with index $($dp.RowCount)"
$dp.SuspendLayout()
$dp.RowCount++
for ($i=0; $i -lt 7; $i++) {
$label = New-Object System.Windows.Forms.Label
$label.TextAlign = "MiddleCenter"
$label.Text = "0"
$label.Font = "Verdana, 9pt"
$label.Size = "$dataSize,$dataHeight"
$label.BackColor = "Transparent"
if ($i -eq 0) {
$label.Size = "$correctedWidth,$dataHeight"
if ($txt -eq $null){
$label.Text = "12345678901234567890123456789012"
} else {
$label.Text = $txt
}
}
$dp.Controls.Add($label, $i, $dp.RowCount - 1)
}
$dp.ResumeLayout()
# If a horizontal scrollbar has just appeared as a follow-on effect
# of AutoScroll having just turned on the vertical scrollbar,
# widen the panel by the width of the vertical scrollbar
# so as to make the horizontal scrollbar disappear again.
if ($dp.HorizontalScroll.Visible) {
$dp.width += [System.Windows.Forms.SystemInformation]::VerticalScrollBarWidth
}
}
旁注:同时使用[void]
和|加载程序集的语句中的Out-Null
是不必要的,并且在PSv5中实际上会失败(也可能是4或3)。