Jey10 Jey10 - 3 months ago 25
PowerShell Question

Can't display groupbox in my Powershell GUI

I don't understand why my GroupBox isn't displayed.
I would like to display groupbox with several labels but it doesn't work.
I've tried to replace the radiobuttons by labels with the code here => https://sysadminemporium.wordpress.com/2012/12/07/powershell-gui-for-your-scripts-episode-3/
I'm a beginner in GUI and I have seen other examples but I can't use this code as I would like.
Here is my code :

#----------------------------------------------
# Generated Form Function
#----------------------------------------------
function Call-test_psf {

#----------------------------------------------
#region Import the Assemblies
#----------------------------------------------
[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
#endregion Import Assemblies

#----------------------------------------------
#region Generated Form Objects
#----------------------------------------------
[System.Windows.Forms.Application]::EnableVisualStyles()
$form1 = New-Object 'System.Windows.Forms.Form'

$form1.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$form1.MaximizeBox = $False
$form1.MinimizeBox = $False
# Choix du titre
$form1.Text = "Title"

$label4 = New-Object 'System.Windows.Forms.Label'
$label5 = New-Object 'System.Windows.Forms.Label'



#endregion Generated Form Objects

#----------------------------------------------
# User Generated Script
#----------------------------------------------


$form1_Load={
#TODO: Initialize Form Controls here

}

# --End User Generated Script--
#----------------------------------------------
#region Generated Events
#----------------------------------------------

$Form_StateCorrection_Load=
{
#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}

$Form_Cleanup_FormClosed=
{
#Remove all event handlers from the controls
try
{
$buttonSuivant.remove_Click($buttonSuivant_Click)
$buttonRetour.remove_Click($buttonRetour_Click)
$form1.remove_Load($form1_Load)
$form1.remove_Load($Form_StateCorrection_Load)
$form1.remove_FormClosed($Form_Cleanup_FormClosed)
}
catch [Exception]
{ }
}
#endregion Generated Events

#----------------------------------------------
#region Generated Form Code
#----------------------------------------------
$form1.SuspendLayout()
#
# form1
#
$form1.Controls.Add($groupBox1)
$form1.ClientSize = '700, 300'

#endregion
$form1.Name = 'form1'
$form1.Text = 'Title'
$form1.add_Load($form1_Load)
#
# GroupBox1
#
$groupBox1 = New-Object System.Windows.Forms.GroupBox
$groupBox1.Location = '150,300'
$groupBox1.size = '400,150'
$groupBox1.text = "Title groupBox1"
$groupBox1.Visible = $true
#
# progressBar1
#
$progressBar1 = New-Object System.Windows.Forms.ProgressBar
$progressBar1.Name = 'progressBar1'
$progressBar1.Value = 0
$progressBar1.Style="Continuous"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = $width - 40
$System_Drawing_Size.Height = 20
$progressBar1.Size = $System_Drawing_Size
$progressBar1.Left = 5
$progressBar1.Top = 40
#
# label4
#
$label4.Font = 'Microsoft Sans Serif, 8pt'
$label4.Location = '50, 50'
$label4.Name = 'label2'
$label4.Size = '20, 20'
$label4.TabIndex = 8
$label4.TextAlign = 'TopLeft'
$label4.Visible = $false
$label4.Text = "test"
#
# label5
#
$label5.Font = 'Microsoft Sans Serif, 8pt'
$label5.Location = '50, 70'
$label5.Name = 'label2'
$label5.Size = '20, 20'
$label5.TabIndex = 10
$label5.TextAlign = 'TopLeft'
$label5.Visible = $false
$label5.Text = "test"

$groupBox1.Controls.AddRange(@($Label4,$Label5))
#
$form1.ResumeLayout()
#endregion Generated Form Code

#----------------------------------------------

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($Form_StateCorrection_Load)
#Clean up the control events
$form1.add_FormClosed($Form_Cleanup_FormClosed)
#Show the Form
return $form1.ShowDialog()

} #End Function

#Call the form
Call-test_psf | Out-Null


Can you help me ?
Thank you in advance.

Answer

Change 2 lines:

$groupBox1.Location = '150,300'

y location is 300, which is off the bottom of your form as it's only 300px high.

$form1.Controls.Add($groupBox1)

Move this line to after you have created the groupbox. So anywhere after $groupBox1 = New-Object System.Windows.Forms.GroupBox

For example:

$groupBox1 = New-Object System.Windows.Forms.GroupBox
$groupBox1.Location = '10,10' 
$groupBox1.size = '400,150'
$groupBox1.text = "Title groupBox1"
$groupBox1.Visible = $true
$form1.Controls.Add($groupBox1)           # line moved here.