GUI - Display data - table format - color? how?

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 5 years and 4 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GUI - Display data - table format - color? how?

Post by jvierra »

Enumerate the "Items" property and create custom objects that can be exported
User avatar
lontru
Posts: 103
Last visit: Thu Mar 07, 2024 2:00 pm

Re: GUI - Display data - table format - color? how?

Post by lontru »

Code: Select all

$button_export_data_Click={
	#TODO: Place custom script here
	$listview_sysinfo.Items | Select-Object -ExpandProperty SubItems |Export-Csv -Path "C:\tmp\LookupBug.txt"
}
How do i only get the value? i get a lot :

Image
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GUI - Display data - table format - color? how?

Post by jvierra »

You have to extract the text of each subitem.

Code: Select all

$listview_sysinfo.Items | 
    select -expand SubItems |
        select Name, Text
    }
User avatar
lontru
Posts: 103
Last visit: Thu Mar 07, 2024 2:00 pm

Re: GUI - Display data - table format - color? how?

Post by lontru »

jvierra wrote: Fri Nov 09, 2018 2:38 am You have to extract the text of each subitem.

Code: Select all

$listview_sysinfo.Items | 
    select -expand SubItems |
        select Name, Text
    }
Thanks a little side question: when runing the gui application. Can i access a terminal so i can debug what happening in the script
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GUI - Display data - table format - color? how?

Post by jvierra »

PowerShell Studio has a built in debugger.

I recommend starting by reading the product manual. You can access it on the help tab.
User avatar
lontru
Posts: 103
Last visit: Thu Mar 07, 2024 2:00 pm

Re: GUI - Display data - table format - color? how?

Post by lontru »

jvierra wrote: Fri Nov 09, 2018 2:38 am You have to extract the text of each subitem.

Code: Select all

$listview_sysinfo.Items | 
    select -expand SubItems |
        select Name, Text
    }
with above code i get every item in a list.

I want to get it so i can pair the property and value together? on the same line?
How do i select columm `?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GUI - Display data - table format - color? how?

Post by jvierra »

That is just an example of what properties are on a ListViewItem. You need to use them to create a custom object and output that.

Start with an empty hash and add the "Name" and "Text" to the hash then convert to an object.

$hash = @{}
$Hash.Add{$_.Name,$_.Text}


After all subitems are added just co vert an output in one line:

[pscustomobject]$hash
User avatar
lontru
Posts: 103
Last visit: Thu Mar 07, 2024 2:00 pm

Re: GUI - Display data - table format - color? how?

Post by lontru »

Im going in circle what ever i do i get the a list out

How to i get the items and subitems... all value are under Text?

Code: Select all

$button_export_data_Click = {
	
	$Export_sysinfo = @()
	$collection = $listview_sysinfo.Items | Select-Object -ExpandProperty SubItems | select Text
	
	foreach ($items in $collection)
	{
		foreach ($item in $items)
		{
			$item | Out-File -FilePath "V:\outfile.txt" -Append
		}
	}

}
could you give some pointers
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GUI - Display data - table format - color? how?

Post by jvierra »

Where is the hash? Where is the custom object?

You have to enumerate "Items" and "SubItems" and add the "Text" to the hash keyed by "Name". See above example of how to use a hash.

I wiIl search and see if I can find an example.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GUI - Display data - table format - color? how?

Post by jvierra »

Well, as I suspected, no one seems to ever have decided to export the contents of a ListView. The issue is that ListView is not a data aware control and has no sense of data. It is just a bag of controls designed to allow different methods of viewing information. There are many third party ListView controls that support "data binding". Normally we would use one of those or crate a custom control to do this.

In PowerShell it is important to learn how to enumerate complex object collections, Once you can master this fundamental skill most things in PowerShell become much easier. To that end I will give you a mii tutorial.

First enumerate the "Items" collection and add the item to a new hash.

Code: Select all

$listview_sysinfo.Items | 
    ForEach-Object{
        $hash = @{}
        $hash.Add($_.Name,$_.Text)
    }
}
Next create a custom object from the hash and export it:

Code: Select all

$listview_sysinfo.Items | 
    ForEach-Object{
        $hash = @{}
        $hash.Add($_.Name,$_.Text)
    } |
    Export-Csv $filename
This gets you all of the items with the name used to create the item as the property and the text of the item as the value and create a collection of objects that are output to a Csv file. This is the classic "design pattern" for enumerating and exporting a complex object.

See: https://en.wikipedia.org/wiki/Software_design_pattern

All of the world of OOP (Object Oriented Programming) is implemented around this concept. Windows, the Net Framework and PowerShell are all object systems.

From the basic pattern we can now add a second level of generalization that we target at the "SubItems" collection to add it to our custom object. The total code required is on simple pipeline.

$_.SubItems | %{$hash.Add($_.Name,$_.Text)}{/b]

The full generalized implementation for exporting a ListView is as follows.

Code: Select all

$listview_sysinfo.Items | 
    ForEach-Object{
        $hash = @{}
        $hash.Add($_.Name,$_.Text)
        $_.SubItems | %{$hash.Add($_.Name,$_.Text)}
        [pscustomobject]$hash
    } |
    Export-Csv $filename
An easy way to work out these things is to export the PSF to a PS1 file and run with PowerShell. If you edit the exported ps1 and make the "$listview_sysinfo" variable a "global" it will persist after the form closes and you can then test manipulating this object interactively at a prompt. This method is a valuable tool for interactively inspecting and testing code against a complex object until you understand how you need to build the code in the form.

Good luck.
This topic is 5 years and 4 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