Hiding file in list box

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.
This topic is 6 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Hiding file in list box

Post by sekou2331 »

I have a list box that goes to a path and shows all files in the path. I want to hide a certain file from being seen in the list box GUI. Is there a way to hide from view. I am still using the file in the code. Please see the code below I am using load the list of files into the GUI.

Code: Select all

$formAPP_Load = {
	Load-FileList  -FileListFolder  '\C:\allFiles\'	
}

function Load-FileList {
	param
	(
		[Parameter(Mandatory = $true)]
		[ValidateNotNull()]
		[System.IO.DirectoryInfo]$FileListFolder
	)
	
	$products = gci -Path $FileListFolder -Recurse -File
	
	$leftListbox.SuspendLayout();
	
	foreach ($file in $files) {
		$leftListbox.Items.Add($file)
	}
	
	$leftListbox.ResumeLayout();
}
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Hiding file in list box

Post by mxtrinidad »

When you use 'get-childitem', you'll have the file "extention" property available. This way you can narrow the selection to be listed.
Of course, it's possible to use both the '-filter' parameter or use the "... | where-object{.." to narrow the files.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Hiding file in list box

Post by sekou2331 »

I am getting the error below. I thought using a star and the ext I wanted would fix the issue since I am using gci in the Load-FileList function. But it is giving the below error

ERROR: Load-FileList  : Cannot process argument transformation on parameter 'FileListFolder'. Cannot convert value "C:\allFiles\*.txt" to
ERROR: type "System.IO.DirectoryInfo". Error: "Illegal characters in path."
APP.psf (3, 34): ERROR: At Line: 3 char: 34
ERROR: + ... -FileList   -FileListFolder "C:\allFiles\*.txt"
ERROR: +                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidData: (:) [Load-FileList ], ParameterBindingArgumentTransformationException
ERROR:     + FullyQualifiedErrorId : ParameterArgumentTransformationError,Load-FileList 
ERROR:

Code: Select all

$formAPP_Load = {
	Load-FileList  -FileListFolder  "C:\allFiles\*.txt"
}

function Load-FileList {
	param
	(
		[Parameter(Mandatory = $true)]
		[ValidateNotNull()]
		[System.IO.DirectoryInfo]$FileListFolder
	)
	
	$products = gci -Path $FileListFolder -Recurse -File
	
	$leftListbox.SuspendLayout();
	
	foreach ($file in $files) {
		$leftListbox.Items.Add($file)
	}
	
	$leftListbox.ResumeLayout();
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Hiding file in list box

Post by jvierra »

If you need to selectively show and hide or selectively show files in a listbox or other data bound controls then you can just set the filter on the datatable the control is bound to. This saves requerying for data. The datatable has a defaultview with a "RowFilter" property that can be set to filter the visible rows:

See: https://msdn.microsoft.com/en-us/librar ... .110).aspx

This is one of the many advantages of using data binding with forms.

See: http://tech-comments.blogspot.com/2017/ ... forms.html

Any PowerShell collection can be easily converted to a DataTable Using the provided helper "ConvertTo-DataTable".
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Hiding file in list box

Post by jvierra »

Remove the type from the parameter. It is wrong for a folder. A folder name cannot have a wild card.

Try the following:

[System.IO.DirectoryInfo]'c:\test'
and
[System.IO.DirectoryInfo]'c:\test\*.txt'

The second will throw your error.

This is a case where you are adding an unnecessary function. Just load the ListBox.

Code: Select all

$formAPP_Load = {
	$files = Get-ChildItem C:\allFiles\*.txt -FIle
	$leftListbox.DataSource = ConvertTo-DataTable $files
	$leftlistbox.DisplayMember = 'Name'
}
Note that "SuspendLayout" is used to control flicker on slow processors. Using a table to load a list box does not cause flicker on nearly all modern systems. Using "Add" to load a very large ListBox may cause some small amount of flicker. I recommend not using it and not using "Add" when not needed. Use an object data source or table.
This topic is 6 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked