Home > OS >  PowerShell/Windows Forms - How to add ScrollAble control to a tab?
PowerShell/Windows Forms - How to add ScrollAble control to a tab?

Time:11-03

I'm creating a form with WinForms and I want to add a scrollable area with multiple Labels and TextBoxes.

Here's my code for the form (there's more to it that's specific to my project, but this should be the gist of it):

$form                              = New-Object system.Windows.Forms.Form
$form.ClientSize                   = New-Object System.Drawing.Point(350,380)

$tabcontrol                        = New-object System.Windows.Forms.TabControl 
$tabcontrol.Size                   = New-Object System.Drawing.Point(330,330)
$tabcontrol.Location               = New-Object System.Drawing.Point(10,10)
$form.Controls.Add($tabcontrol)

$tab                               = New-object System.Windows.Forms.Tabpage
$tab.Text                          = "Tab1"
$tabcontrol.Controls.Add($tab)

I've tried adding a ScrollableControl to $tab:

$scroll                            = New-Object System.Windows.Forms.ScrollableControl
$scroll.AutoScroll                 = $true
$scroll.AutoScrollMinSize          = New-Object System.Drawing.Size (0, 200)
$scroll.VerticalScroll.LargeChange = 20
$scroll.VerticalScroll.SmallChange = 7
$tab.Controls.Add($scroll)

When I launch the form, there's no scroll bar or anything on Tab1 ($tab). I've tried adding controls to $scroll, like a System.Windows.Forms.Label or System.Windows.Forms.TextBox, but still nothing.

So what am I doing wrong? How would I add a scrollable control to $tab?

CodePudding user response:

Instead of adding a ScrollableControl, you need to make sure the tab page is scrollable. Add this (with $tab being the Tab page):

$tab.AutoScroll = true
$tab.Size = New-Object System.Drawing.Size (1000, 1000)

This will automatically provide a scrollbar when the Window is smaller than the content of the page.

  • Related