I am trying to simplify the usage of GUI in powershell for a project.
I use [System.Windows.Forms.Label] and [System.Windows.Forms.GroupBox]. I am trying to automate the creation of object and their size. Main goal is to resize parent container I add a child object. In my case, I am trying to add Label inside a GroupBox using theses functions:
function CreateGroupContainer ($Title){
$GroupContainer = New-Object System.Windows.Forms.GroupBox
$GroupContainer.text = $Title
$GroupContainer.Size = New-Object System.Drawing.Size (0,0)
return $GroupContainer
}
function CreateLabel($Text){
$label = New-Object System.Windows.Forms.Label
$label.Size = New-Object System.Drawing.Size(($Text.Lenght \* 100),20)
$label.Text = $Text
return $label
}
function AddLabelToContainer{
param(
\[System.Windows.Forms.Label\] $Label,
\[System.Windows.Forms.GroupBox\] $Container
)
$ContainerWidth = $Container.Size.Width
if($Object.Size.Width \> $ContainerWidth) {
$ContainerWidth = ($Object.Size.Width + 20)
}
$Container = ($Container.Size.Height + $Label.Size.Height + 20)
$Container.Size = New-Object System.Drawing.Size ($ContainerWidth,$ContainerHeight)
$Container.Controls.Add($Label)
return $Container
}
The main calls are:
$LabelAutor = CreateLabel('Text')
$ContainerAutor = CreateGroupContainer('Autor')
$ContainerAutor = AddLabelToContainer($LabelAutor,$ContainerAutor)
It fails on the function AddLabelToContainer with error: AddLabelToContainer : Cannot process argument transformation on parameter 'Label'. Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Windows.Forms.Label". At C:\Users\louis.meot\Accenture\DCW - Service Offers Documents\Cloud\Azure AD Applications Audit\Dialog.ps1:60 char:42
- ... lToContainer([System.Windows.Forms.Label]($LabelAutor),$ContainerAuto ...
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : InvalidData: (:) [AddLabelToContainer], ParameterBindingArgumentTransformationException
- FullyQualifiedErrorId : ParameterArgumentTransformationError,AddLabelToContainer
I am a newbie on Powershell and I tried everything that I understand. I tried to cast, rename and other stuff but without success...