none
Powershell Docked Objects Size RRS feed

  • Question

  • I have tried Googling how to set the size on a docked object (groupbox). But to no avail.

    I am trying to build a GUI interface for frequent tasks. I just don't seem to be able to specify the sizes of the group boxes when the are docked. The sidebar is far too wide and needs to be almost half what it is. I have tried everything I can think of but can't seem to find a way to make it work.

     
    function CreateForm {
    #[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
    #[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.drawing
    $form1 = New-Object System.Windows.Forms.Form
    $Ping_Btn = New-Object System.Windows.Forms.Button
    $CPing_Btn = New-Object System.Windows.Forms.Button
    $TabControl = New-object System.Windows.Forms.TabControl
    $WSUS_Tab = New-Object System.Windows.Forms.TabPage
    $InstalledSoftware_Tab = New-Object System.Windows.Forms.TabPage
    $Info_GB = New-Object System.Windows.Forms.Groupbox
    $Sidebar_GB =  New-Object System.Windows.Forms.Groupbox
    $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
    $form1.Text = "Maintenance Utility"
    $form1.Name = "Form1"
    $form1.DataBindings.DefaultDataSourceUpdateMode = 0
    $System_Drawing_Size = New-Object System.Drawing.Size(725,450)
    $form1.ClientSize = $System_Drawing_Size
        $Form1.KeyPreview = $True 
        $Form1.Add_KeyDown({if ($_.KeyCode -eq "Escape")
            {$Form1.Close()}})
        $Form1.Add_Resize({TC_Refresh})
        $Form1.ShowInTaskbar = $True
    $tabControl.DataBindings.DefaultDataSourceUpdateMode = 0
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 0
    $System_Drawing_Point.Y = 85
    $tabControl.Location = $System_Drawing_Point
    $tabControl.Name = "tabControl1"
    $tabControl.Dock = "Fill"
    $System_Drawing_Size = New-Object System.Drawing.Size(950,300)
    $tabControl.Multiline = $true
    $form1.Controls.Add($tabControl)
    $WSUS_Tab.DataBindings.DefaultDataSourceUpdateMode = 0
    $WSUS_Tab.UseVisualStyleBackColor = $True
    $WSUS_Tab.Name = "WSUS_Tab"
    $WSUS_Tab.Text = "WSUS”
    $tabControl.Controls.Add($WSUS_Tab)
    $InstalledSoftware_Tab.DataBindings.DefaultDataSourceUpdateMode = 0
    $InstalledSoftware_Tab.UseVisualStyleBackColor = $True
    $InstalledSoftware_Tab.Name = "InstalledSoftware_Tab"
    $InstalledSoftware_Tab.Text = "Installed Software”
    $tabControl.Controls.Add($InstalledSoftware_Tab)
    $Info_GB.Location = New-Object System.Drawing.Point(0,0)  
    $Info_GB.Size = New-Object System.Drawing.Size(750,60)  
    $Info_GB.Dock = "Top"
    $Info_GB.BringToFront()
    $Info_GB.Text = "System Information"
    $form1.Controls.Add($Info_GB)
    $System_Drawing_Point = New-Object System.Drawing.Point(751,0)
    $Sidebar_GB.Location = $System_Drawing_Point
    $System_Drawing_Size = New-Object System.Drawing.Size(210,25)
    $Sidebar_GB.Size = $System_Drawing_Size
    $Sidebar_GB.Dock = "Right"
    $Sidebar_GB.SendToBack()
    $Sidebar_GB.Text = "System Functions"
    $form1.Controls.Add($Sidebar_GB)
    $Ping_Btn.TabIndex = 4
    $Ping_Btn.Name = "Ping_Btn"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 35
    $System_Drawing_Size.Height = 25
    $Ping_Btn.Size = $System_Drawing_Size
    $Ping_Btn.UseVisualStyleBackColor = $True
    $Ping_Btn.Text = "Ping"
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 92 #350
    $System_Drawing_Point.Y = 15 #45
    $Ping_Btn.Location = $System_Drawing_Point
    $Ping_Btn.DataBindings.DefaultDataSourceUpdateMode = 0
    $Ping_Btn.add_Click($button1_RunOnClick)
    $Sidebar_GB.Controls.Add($Ping_Btn)
    $CPing_Btn.TabIndex = 5
    $CPing_Btn.Name = "CPing_Btn"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 90
    $System_Drawing_Size.Height = 25
    $CPing_Btn.Size = $System_Drawing_Size
    $CPing_Btn.UseVisualStyleBackColor = $True
    $CPing_Btn.Text = "Continous Ping"
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 2
    $System_Drawing_Point.Y = 15
    $CPing_Btn.Location = $System_Drawing_Point
    $CPing_Btn.DataBindings.DefaultDataSourceUpdateMode = 0
    $CPing_Btn.add_Click($CPing_Btn_RunOnClick)
    $Sidebar_GB.Controls.Add($CPing_Btn)
    $OnLoadForm_StateCorrection={
     $form1.WindowState = $InitialFormWindowState }
     
    $InitialFormWindowState = $form1.WindowState
    $form1.add_Load($OnLoadForm_StateCorrection)
    $form1.ShowDialog()| Out-Null
    }
    CreateForm
     


    Wednesday, June 19, 2019 9:03 PM

All replies

  • When posting in a technical fprum please use the code posting tool provided. Please edit you original post and fix this.

    First remove all unnecessary code and just set the size of the GB.

    function CreateForm {
        
        Add-Type -AssemblyName System.Windows.Forms
        
        $form1 = New-Object System.Windows.Forms.Form
        $Ping_Btn = New-Object System.Windows.Forms.Button
        $CPing_Btn = New-Object System.Windows.Forms.Button
        $TabControl = New-object System.Windows.Forms.TabControl
        $WSUS_Tab = New-Object System.Windows.Forms.TabPage
        $InstalledSoftware_Tab = New-Object System.Windows.Forms.TabPage
        $Info_GB = New-Object System.Windows.Forms.Groupbox
        $Sidebar_GB = New-Object System.Windows.Forms.Groupbox
        
        $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
        
        $form1.Text = 'Maintenance Utility'
        $form1.Name = 'Form1'
        $form1.StartPosition = 'CenterScreen'
        $form1.ClientSize = '725, 450'
        $Form1.KeyPreview = $True
        $Form1.Add_KeyDown({
            if ($_.KeyCode -eq 'Escape') {
                $Form1.Close()
            }
        })
        $Form1.Add_Resize({
            TC_Refresh
        })
        $Form1.ShowInTaskbar = $True
        
        $tabControl.Location = '0,85'
        $tabControl.Name = 'tabControl1'
        $tabControl.Dock = 'Fill'
        $tabControl.Multiline = $true
        $form1.Controls.Add($tabControl)
        
        $WSUS_Tab.UseVisualStyleBackColor = $True
        $WSUS_Tab.Name = 'WSUS_Tab'
        $WSUS_Tab.Text = 'WSUS'
        $tabControl.Controls.Add($WSUS_Tab)
        
        #Installed Software
        $InstalledSoftware_Tab.DataBindings.DefaultDataSourceUpdateMode = 0
        $InstalledSoftware_Tab.UseVisualStyleBackColor = $True
        $InstalledSoftware_Tab.Name = 'InstalledSoftware_Tab'
        $InstalledSoftware_Tab.Text = 'Installed Software'
        $tabControl.Controls.Add($InstalledSoftware_Tab)
        
        $Info_GB.Location = '0, 0'
        $Info_GB.Size = '750, 60'
        $Info_GB.Dock = 'Top'
        $Info_GB.BringToFront()
        $Info_GB.Text = 'System Information'
        $form1.Controls.Add($Info_GB)
        
        $Sidebar_GB.Location = '751, 0'
        $Sidebar_GB.Size = '170,25'
        $Sidebar_GB.Dock = 'Right'
        $Sidebar_GB.SendToBack()
        $Sidebar_GB.Text = 'System Functions'
        $form1.Controls.Add($Sidebar_GB)
        
        $Ping_Btn.TabIndex = 4
        $Ping_Btn.Name = 'Ping_Btn'
        $Ping_Btn.Size = '35,25'
        $Ping_Btn.UseVisualStyleBackColor = $True
        $Ping_Btn.Text = 'Ping'
        $Ping_Btn.Location = '92,15'
        $Ping_Btn.add_Click($button1_RunOnClick)
        $Sidebar_GB.Controls.Add($Ping_Btn)
        
        $CPing_Btn.TabIndex = 5
        $CPing_Btn.Name = 'CPing_Btn'
        $CPing_Btn.Size = '90,25'
        $CPing_Btn.UseVisualStyleBackColor = $True
        $CPing_Btn.Text = 'Continous Ping'
        $CPing_Btn.Location = '2,15'
        $CPing_Btn.add_Click($CPing_Btn_RunOnClick)
        $Sidebar_GB.Controls.Add($CPing_Btn)
        
        $OnLoadForm_StateCorrection = {
            $form1.WindowState = $InitialFormWindowState
        }
        
        $InitialFormWindowState = $form1.WindowState
        $form1.add_Load($OnLoadForm_StateCorrection)
        $form1.ShowDialog() | Out-Null
        
    }
    CreateForm


    \_(ツ)_/

    Wednesday, June 19, 2019 10:07 PM
  • Thanks for the suggestion. However the code you provided does not dock the groupbox to the right side. I need this window form to be dynamically sized depending on the users needs. Specifying the size would require a great deal of unnecessary code when a simple .dock method will do it. This works well in DotNet. I just need to know how to specify the width/height of the dock in powershell. This does not seem to be documented anywhere.
    Thursday, June 20, 2019 1:05 PM
  • "Docking" does not do what you think.  "Anchor" does what you are asking.  If you anchor to the "right,top,bottom" the box will resize with the form.

    See: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.anchor?view=netframework-4.8


    \_(ツ)_/

    Thursday, June 20, 2019 1:50 PM