Click here to Skip to main content
15,888,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to create a checkbox automatically depends on folder I selected. I create ComboBox, then in the ComboBox, I can select which folder that I want to select. Inside of my folder, I have some file. The file consist of some extension file. I just need to pick 2 extension file from the folder, example(*.txt and *.csv).

After I select the folder, the checkBox will create automatically, the total of the checkBox depends on how many file exist in that folder with specific extension(*.txt and *.csv).

In my code, I already do some stuff, which is select the folder that I need to select, but still struggle with the checkBox. Anyone can help me please. Thank you so much. I really appreciate for the help.

What I have tried:

Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

# $Global:status = "inactive" 
# $Global:array = New-Object System.Collections.Generic.List[System.Object]

$Form                            = New-Object system.Windows.Forms.Form
$Form.BackColor                  = "#f6f6f6"
$Form.AutoSize                   = $true
$Form.FormBorderStyle            = "FixedDialog"
$Form.MaximizeBox                = $false
$Form.startposition              = "centerscreen"
$Form.WindowState                = 'Maximized'

$Label1                          = New-Object system.Windows.Forms.Label
$Label1.text                     = "FOLDER"
$Label1.AutoSize                 = $true
$Label1.width                    = 25
$Label1.height                   = 10
$Label1.location                 = New-Object System.Drawing.Point(35,80)
$Label1.Font                     = 'Microsoft Sans Serif,12,style=Bold'
$Label1.ForeColor                = "#000000"

$Button3                         = New-Object system.Windows.Forms.Button
$Button3.BackColor               = "#136aa4"
$Button3.ForeColor               = "#ffffff"
$Button3.text                    = "Done"
$Button3.width                   = 90
$Button3.height                  = 32
$Button3.UseCompatibleTextRendering = $True
$Button3.location                = New-Object System.Drawing.Point(1700,920)
$Button3.Font                    = 'Microsoft Sans Serif,10'
$Button3.Visible                 = $false

$Button2                         = New-Object system.Windows.Forms.Button
$Button2.BackColor               = "#136aa4"
$Button2.ForeColor               = "#ffffff"
$Button2.text                    = "Delete"
$Button2.width                   = 90
$Button2.height                  = 32
$Button2.UseCompatibleTextRendering = $True
$Button2.location                = New-Object System.Drawing.Point(1600,920)
$Button2.Font                    = 'Microsoft Sans Serif,10'
$Button2.Visible                 = $false

$Panel = New-Object System.Windows.Forms.TableLayoutPanel
$panel.Dock = "Fill"
$panel.ColumnCount = 1
$panel.RowCount = 1
$panel.CellBorderStyle = "single"
$panel.ColumnStyles.Add((new-object System.Windows.Forms.ColumnStyle([System.Windows.Forms.SizeType]::Percent, 100)))
$panel.RowStyles.Add((new-object System.Windows.Forms.RowStyle([System.Windows.Forms.SizeType]::Percent, 100)))


$Groupbox1                       = New-Object system.Windows.Forms.Groupbox
$Groupbox1.text                  = "Handling"
$Groupbox1.Font                  = 'Microsoft Sans Serif,9'
$Groupbox1.AutoSize              = $true
$Groupbox1.ForeColor             = "#032d5d"
$Groupbox1.location              = New-Object System.Drawing.Point(8,13)
$Groupbox1.Padding               = New-Object -TypeName System.Windows.Forms.Padding -ArgumentList (0,5,5,0)
$Groupbox1.Dock                  = "fill"

$Groupbox2                       = New-Object system.Windows.Forms.Groupbox
$Groupbox2.text                  = "Information"
$Groupbox2.Font                  = 'Microsoft Sans Serif,9'
$Groupbox2.AutoSize              = $true
$Groupbox2.ForeColor             = "#032d5d"
$Groupbox2.Dock                  = "None"

$Groupbox1.Height
$Groupbox1.Width
$g2w = $Groupbox1.Width * 9.2
$g2h = $Groupbox1.Height * 7

$Groupbox2.location              = New-Object System.Drawing.Point(35,203)
$Groupbox2.size                  = New-Object System.Drawing.Size($g2w,$g2h)


$ComboBox1                        = New-Object system.Windows.Forms.ComboBox
$ComboBox1.BackColor              = "#e8f3ff"
$ComboBox1.width                  = 180
$ComboBox1.height                 = 20
$ComboBox1.location               = New-Object System.Drawing.Point(100,75)
$ComboBox1.Font                   = 'Microsoft Sans Serif,12'
$ComboBox1.AutoSize               = $true
$ImageList = @(Get-ChildItem -Directory "D:\Process")
foreach ($img in $ImageList) {
    $ComboBox1.Items.Add($img)
}


$Form.controls.AddRange(@($Panel))
$Panel.controls.AddRange(@($Groupbox1))
$Groupbox1.Controls.AddRange(@($Groupbox2, $ComboBox1, $Label1, $Button3, $Button2))

[void]$Form.ShowDialog()




###Updated
I update my script. But the file only appear 1, once I click the folder. And when I change to click other folder, It does not appear the file.

Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Global:status = "inactive" 
$Global:array = New-Object System.Collections.Generic.List[System.Object]

$Form                            = New-Object system.Windows.Forms.Form
$Form.text                       = "BPS Image Automation Utility"
$Form.BackColor                  = "#f6f6f6"
$Form.AutoSize                   = $true
$Form.FormBorderStyle            = "FixedDialog"
$Form.MaximizeBox                = $false
$Form.startposition              = "centerscreen"
$Form.WindowState                = 'Maximized'

$Label1                          = New-Object system.Windows.Forms.Label
$Label1.text                     = "Please select the image"
$Label1.AutoSize                 = $true
$Label1.width                    = 25
$Label1.height                   = 10
$Label1.location                 = New-Object System.Drawing.Point(50,50)
$Label1.Font                     = 'Microsoft Sans Serif,10'
$Label1.ForeColor                = "#000000"
$label1.AutoSize                 = $true

$Button3                         = New-Object system.Windows.Forms.Button
$Button3.BackColor               = "#136aa4"
$Button3.ForeColor               = "#ffffff"
$Button3.text                    = "Done"
$Button3.width                   = 90
$Button3.height                  = 32
$Button3.AutoSize = $true
$Button3.UseCompatibleTextRendering = $True
$Button3.UseVisualStyleBackColor = $False
# $Button3.location                = New-Object System.Drawing.Point(1700,920)
$Button3.Font                    = 'Microsoft Sans Serif,10'
# $Button3.Visible                 = $false

$Button2                         = New-Object system.Windows.Forms.Button
$Button2.BackColor               = "#136aa4"
$Button2.ForeColor               = "#ffffff"
$Button2.text                    = "Delete"
$Button2.width                   = 90
$Button2.height                  = 32
$Button2.UseCompatibleTextRendering = $True
$Button2.UseVisualStyleBackColor = $False
$Button2.AutoSize = $true
# $Button2.location                = New-Object System.Drawing.Point(1600,920)
$Button2.Font                    = 'Microsoft Sans Serif,10'
# $Button2.Visible                 = $false

$Panel = New-Object System.Windows.Forms.TableLayoutPanel
$panel.Dock = "Fill"
$panel.ColumnCount = 1
$panel.RowCount = 1
$panel.CellBorderStyle = "single"
$panel.ColumnStyles.Add((new-object System.Windows.Forms.ColumnStyle([System.Windows.Forms.SizeType]::Percent, 100)))
$panel.RowStyles.Add((new-object System.Windows.Forms.RowStyle([System.Windows.Forms.SizeType]::Percent, 100)))


$Groupbox1                       = New-Object system.Windows.Forms.Groupbox
$Groupbox1.text                  = "Job Handling"
$Groupbox1.Font                  = 'Microsoft Sans Serif,9'
$Groupbox1.AutoSize              = $true
$Groupbox1.ForeColor             = "#032d5d"
$Groupbox1.location              = New-Object System.Drawing.Point(8,13)
$Groupbox1.Padding               = New-Object -TypeName System.Windows.Forms.Padding -ArgumentList (0,5,5,0)
$Groupbox1.Dock                  = "fill"
$Groupbox1.UseCompatibleTextRendering = $True

$Groupbox2                       = New-Object system.Windows.Forms.Groupbox
$Groupbox2.text                  = "Job Information"
$Groupbox2.Font                  = 'Microsoft Sans Serif,9'
$Groupbox2.AutoSize              = $true
$Groupbox2.ForeColor             = "#032d5d"
$Groupbox2.Dock                  = "None"
$Groupbox2.UseCompatibleTextRendering = $True

$ComboBox1                        = New-Object system.Windows.Forms.ComboBox
$ComboBox1.BackColor              = "#e8f3ff"
$ComboBox1.width                  = 190
$ComboBox1.height                 = 20
$ComboBox1.location               = New-Object System.Drawing.Point(35,80)
$ComboBox1.Font                   = 'Microsoft Sans Serif,12'
$ComboBox1.AutoSize               = $true
$ImageList = @(Get-ChildItem -Directory "D:\Process")
foreach ($img in $ImageList) {
    $ComboBox1.Items.Add($img)
}
$ComboBox1.Add_Click({
    if($ComboBox1.SelectedItem){
        $Checkbox.Visible = $true
    }
})

$Checkboxes += New-Object System.Windows.Forms.CheckBox
$Checkboxes = @()
$y = 20
$files = Get-ChildItem "D:\Process\$img" -Filter *.txt, *.csv
$files
foreach ($file in $files)
{    
   
    $Checkbox = New-Object System.Windows.Forms.CheckBox
    $Checkbox.Text = $file
    $Checkbox.Location = New-Object System.Drawing.Size(10,$y) 
    $Checkbox.Size = New-Object System.Drawing.Size(330,20) 
    $y += 30
    $Groupbox2.Controls.Add($Checkbox) 
    $Checkboxes += $Checkbox
    $Checkbox.Visible = $false
}
if($ComboBox1.SelectedItem -eq "$ImageList")
{
    $ImageList
    $Checkbox.Visible = $true
    $Button3.Visible = $true
    $Button2.Visible = $true
}

$Form.controls.AddRange(@($Panel))
$Panel.controls.AddRange(@($Groupbox1))
$Groupbox1.Controls.AddRange(@($Groupbox2, $ComboBox1, $Label1, $Button3, $Button2))

[void]$Form.Show()

$g2w = $Form.Width - 90
$g2h = $Form.Height - 270
$g2h

$Groupbox2.location              = New-Object System.Drawing.Point(35,110)
$Groupbox2.size                  = New-Object System.Drawing.Size($g2w,$g2h)
$Groupbox2.AutoSize              = $true

$bt2_w = $g2w - 55
$bt2_h = $g2h + 130
$Button2.location                = New-Object System.Drawing.Point($bt2_w,$bt2_h)


$bt3_w = $g2w - 160
$Button3.location                = New-Object System.Drawing.Point($bt3_w,$bt2_h)

$Form.Visible = $false
[void]$Form.ShowDialog()
Posted
Updated 28-Nov-19 19:36pm
v5

That sounds like you want a CheckedListBox, see example here: CheckedListBox in VB.NET[^]
 
Share this answer
 
Comments
Maciej Los 28-Nov-19 6:30am    
The question is not about WinForms, but about PowerShell UI.
RickZeeland 28-Nov-19 6:32am    
I know, but the syntax is similar :)
Richard Deeming 28-Nov-19 8:45am    
The OP is using WinForms from Powershell. :)
Check it out: Build Checkbox list programmatically using powershell - Stack Overflow[^]
Note: you need to add groupbox first, then checkboxes to that groupbox.

JavaScript
foreach ($img in $ImageList)
{    
    $Checkbox = New-Object System.Windows.Forms.CheckBox
    $Checkbox.Text = $img
    $Checkbox.Location = New-Object System.Drawing.Size(10,$y) 
    $y += 30
    $groupBox.Controls.Add($Checkbox) 
    $Checkboxes += $Checkbox
}


[EDIT]

Quote:
I update my script. But the file only appear 1, once I click the folder. And when I change to click other folder, It does not appear the file.


When i looked at your updated code, i saw that you're getting only non-system and unhidden files in specific folder. If you would like to get more files, you need to add some switches. See:
JavaScript
Get-ChildItem -Path C:\ -Force -Recurse


More at:
Get-ChildItem[^]
Working with Files and Folders - PowerShell | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
Member 14156312 28-Nov-19 22:20pm    
Hi @MacieJ Los, I tried your code, but the checkBox show only the folder, not the file inside the folder.
and also return this error Method invocation failed because [System.Windows.Forms.CheckBox] does not contain a method named 'op_Addition'.
Maciej Los 29-Nov-19 1:34am    
This is your code! I've shown you only the part of code responsible for checkbox creating.
Member 14156312 29-Nov-19 1:37am    
Ya, I mean I use my code, but I updated with your advice.
Member 14156312 29-Nov-19 3:08am    
This part Get-ChildItem -Path C:\ -Force -Recurse is does not affect anything.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900