arrays - Create and Add new Variable to a CheckedListBox -
i want automate code below , make dynamic, when click on button add should create $template(x)
, should added $templatearray
.
(the $menulist
checkedlistbox
, shows file names)
but
for have $template1
, $template2
, if want 1 have type in code manually want done add button.
$template1 = split-path c:\users\$env:username\documents\test.txt -leaf -resolve $template2 = split-path c:\users\$env:username\documents\test2.txt -leaf -resolve $templatearray = $template1,$template2; $menulist.items.addrange($templatearray) $button_add_onclick = { #todo: place custom script here }
also possible after when click on add asks me file location , going formatted because want show file name in checkedlistbox
want able open file button ( open button not part of question)
split-path c:\users\$env:username\documents\test.txt -leaf -resolve
asking user select file form pretty easy - use openfiledialog
class:
$addbutton.add_click({ # create dialog $openfiledialog = new-object system.windows.forms.openfiledialog # configure behavior liking $openfiledialog.initialdirectory = $pwd $openfiledialog.filter = "text files (*.txt)|*.txt" # launch , wait user select file if($openfiledialog.showdialog() -eq [system.windows.forms.dialogresult]::ok) { # add path of selected file checkedlistbox items $menulist.items.add($openfiledialog.filename) # add templates array $templatearray += $openfiledialog.filename } })
you mention you'll need full path of file later, you're interested in showing name of file user.
you can accomplish adding proper fileinfo
objects (same type get-item , get-childitem returns files) list, rather strings:
$template1 = get-item c:\users\$env:username\documents\test.txt $template2 = get-item c:\users\$env:username\documents\test2.txt $templatearray = $template1,$template2 $addbutton.add_click({ # ... # same before # ... if($openfiledialog.showdialog() -eq [system.windows.forms.dialogresult]::ok) { $targetfile = get-item $openfiledialog.filename # add selected file checkedlistbox items $menulist.items.add($targetfile) # add templates array $templatearray += $targetfile } })
and set displaymember
property of checkedlistbox make sure displays name of file:
$menulist.displaymember = "name"
so in gui, looks have list of strings, when retrieve selecteditems
it's full-fledged fileinfo
object:
ps> $menulist.selecteditems[0].fullname c:\users\codebusta\documents\test.txt
Comments
Post a Comment