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.
Locked
User avatar
sekou2331
Posts: 240
Joined: Wed Aug 15, 2012 6:07 am

Hiding file in list box

Post by sekou2331 » Wed Oct 11, 2017 8:07 am

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
Site Admin
Posts: 45
Joined: Sun Mar 03, 2013 12:42 pm

Re: Hiding file in list box

Post by mxtrinidad » Wed Oct 11, 2017 8:20 am

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: 240
Joined: Wed Aug 15, 2012 6:07 am

Re: Hiding file in list box

Post by sekou2331 » Wed Oct 11, 2017 8:41 am

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();
}

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

Re: Hiding file in list box

Post by jvierra » Wed Oct 11, 2017 8:54 am

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".

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

Re: Hiding file in list box

Post by jvierra » Wed Oct 11, 2017 9:04 am

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.

Locked