Checked List Box Import from CSV

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
A. Hohl
Posts: 18
Joined: Thu Apr 20, 2017 10:33 am

Checked List Box Import from CSV

Post by A. Hohl » Fri Nov 03, 2017 7:47 am

Hello,

I am trying to design a CheckedListBox that will import a list of computer names from a CSV and allow me to later do a "foreach" command to "mass enable" computers that have been disabled in AD. any pointers on how to accomplish this?

Thanks,
Austin

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Checked List Box Import from CSV

Post by jvierra » Fri Nov 03, 2017 7:54 am

Have you looked in the info center for reference on how to use controls
https://info.sapien.com/index.php/guis/gui-controls

User avatar
A. Hohl
Posts: 18
Joined: Thu Apr 20, 2017 10:33 am

Re: Checked List Box Import from CSV

Post by A. Hohl » Fri Nov 03, 2017 9:15 am

The site has a lot of helpful information, I am having a hard time specifically finding the command to import the CSV to the check list though...

Thanks,
Austin

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Checked List Box Import from CSV

Post by jvierra » Fri Nov 03, 2017 9:22 am

It works the same as a ListBox. The checks will not automatically be set. TO do that you need to use a DataTable.
A DataGridView works better.

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Checked List Box Import from CSV

Post by jvierra » Fri Nov 03, 2017 2:15 pm

I couldn't find any examples of how to use a CLB so here is a demo:

The code is simple:

Code: Select all

$csvtxt = @'
Name,Status
X0011,True
X0012,False
X0013,False
X0014,True
'@
$form1_Load={
    $csv = ConvertFrom-Csv $csvtxt
    $checkedlistbox1.DataSource = [System.Collections.ArrayList]$csv
    $checkedlistbox1.DisplayMember = 'Name'
    for($i=0;$i -lt $checkedlistbox1.Items.Count;$i++){
        $state = if($checkedlistbox1.Items[$i].Status -eq 'True'){$true}else{$false}
        $checkedlistbox1.SetItemChecked($i,$state)
    }
}
I used embedded CSV but it can be imported.
Attachments
Demo-CheckedListBoxLoad.psf
(26.18 KiB) Downloaded 53 times

User avatar
LtMandella
Posts: 61
Joined: Fri Feb 12, 2016 5:32 pm

Re: Checked List Box Import from CSV

Post by LtMandella » Fri Nov 03, 2017 3:37 pm

@JV
You ought to scrape this site for all your "how-to" answers, examples, and demos and compile into a book.
I bet you would sell a pile of books! I would buy it! (and in any event thanks for all your continuing help...)
Lt

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Checked List Box Import from CSV

Post by jvierra » Fri Nov 03, 2017 4:10 pm

Thanks. Great idea. Now I need to find an editor and a publisher. Of course by the time I publish five other people will have published the same thing.

A book is a big and time consuming project. I used to write technical sales documents on a Saturday afternoon that went 60 or more pages. I think it was good I had a captive audience. Always an emergency. Always a sale that had to be done in days for a project that could take years.

I do like knocking off quick demos. They require limited effort.

User avatar
A. Hohl
Posts: 18
Joined: Thu Apr 20, 2017 10:33 am

Re: Checked List Box Import from CSV

Post by A. Hohl » Wed Nov 08, 2017 7:32 am

Worked perfectly. Thank you!

Locked