Hello,
here the new version :
function GenerateForm {
param(
[Array[]]$CheckBoxLabelsInstances,
[Array[]]$CheckBoxLabelsModules
)
Keep track of number of CheckBoxesInstances
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$instances = [System.Collections.ArrayList]@()
$modules = [System.Collections.ArrayList]@()
$form1 = New-Object System.Windows.Forms.Form
$form1.Text = "Primal Form"
$form1.Name = "form1"
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 2000
$System_Drawing_Size.Height = 2000
$form1.ClientSize = $System_Drawing_Size
$button1 = New-Object System.Windows.Forms.Button
$button1.Text = "Run Script"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 27
$System_Drawing_Point.Y = 156
$button1.Location = $System_Drawing_Point
$button1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Controls.Add($button1)
$button1.add_Click({
$listBox1.Items.Clear();
# Keep track of whether something has been added to the list
$ContentPresent = $false
# Iterate over the CheckBoxesInstances, one by one
foreach($CheckBoxInstance in $CheckBoxesInstances){
if(!($CheckBoxInstance.Checked)){
write-host $CheckBoxInstance.Text
$instances.remove($CheckBoxInstance.Text)
write-host " nouveau " $instances
$ContentPresent = $True
}
}
})
$CheckBoxCounterInstance = 1
When we create a new textbox, we add it to an array for easy reference later
$CheckBoxesInstances = foreach($LabelInstance in $CheckBoxLabelsInstances) {
$CheckBoxInstance = New-Object System.Windows.Forms.CheckBox
$CheckBoxInstance.UseVisualStyleBackColor = $True
$CheckBoxInstance.Checked = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 104
$System_Drawing_Size.Height = 24
$CheckBoxInstance.Size = $System_Drawing_Size
$CheckBoxInstance.TabIndex = 2
# Assign text based on the input
$CheckBoxInstance.Text = $LabelInstance
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 27
# Make sure to vertically space them dynamically, counter comes in handy
$System_Drawing_Point.Y = 13 + (($CheckBoxCounterInstance - 1) * 31)
$CheckBoxInstance.Location = $System_Drawing_Point
$CheckBoxInstance.DataBindings.DefaultDataSourceUpdateMode = 0
# Give it a unique name based on our counter
$CheckBoxInstance.Name = "CheckBoxInstance$CheckBoxCounterInstance"
# Add it to the form
$form1.Controls.Add($CheckBoxInstance)
# return object ref to array
$CheckBoxInstance
# increment our counter
$CheckBoxCounterInstance++
$instances.Add($CheckBoxInstance.Text)
}
$CheckBoxCounterModule = 1
$CheckBoxesModules = foreach($LabelModule in $CheckBoxLabelsModules) {
$CheckBoxModule = New-Object System.Windows.Forms.CheckBox
$CheckBoxModule.UseVisualStyleBackColor = $True
$CheckBoxModule.Checked = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 104
$System_Drawing_Size.Height = 24
$CheckBoxModule.Size = $System_Drawing_Size
$CheckBoxModule.TabIndex = 2
# Assign text based on the input
$CheckBoxModule.Text = $LabelModule
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 50
# Make sure to vertically space them dynamically, counter comes in handy
$System_Drawing_Point.Y = 13 + (($CheckBoxCounterModule - 1) * 31)
$CheckBoxModule.Location = $System_Drawing_Point
$CheckBoxModule.DataBindings.DefaultDataSourceUpdateMode = 0
# Give it a unique name based on our counter
$CheckBoxModule.Name = "CheckBoxModule$CheckBoxCounterModule"
# Add it to the form
$form1.Controls.Add($CheckBoxModule)
# return object ref to array
$CheckBoxModule
# increment our counter
$CheckBoxCounterModule++
$Modules.Add($CheckBoxModule.Text)
}
write-host "instances : "$instances
$form1.Add_Shown({$Form1.Activate()})
[void] $form1.ShowDialog()
}
$moduleemplacment="D:\PowerShell\modules"
$EmplacementFichier = "D:\PowerShell\instanceliste.txt"
$MonFichier = get-content $EmplacementFichier
$lines= foreach ($UneLigne in $MonFichier){
$UneLigne
}
$modules = Get-ChildItem -Path $moduleemplacment -Directory |
Foreach-Object {
$myobj1name=$_.Name
}
write-host $modules
GenerateForm -CheckBoxLabelsInstances $lines -CheckBoxLabelsModules $modules