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.
User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Thu Nov 08, 2018 5:09 pm

Enumerate the "Items" property and create custom objects that can be exported

User avatar
lontru
Posts: 58
Joined: Fri Aug 18, 2017 4:36 am

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

Post by lontru » Fri Nov 09, 2018 3:27 am

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

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

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

Post by jvierra » Fri Nov 09, 2018 3:38 am

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: 58
Joined: Fri Aug 18, 2017 4:36 am

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

Post by lontru » Fri Nov 09, 2018 4:50 am

jvierra wrote:
Fri Nov 09, 2018 3: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

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

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

Post by jvierra » Fri Nov 09, 2018 5:00 am

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: 58
Joined: Fri Aug 18, 2017 4:36 am

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

Post by lontru » Fri Nov 09, 2018 6:43 am

jvierra wrote:
Fri Nov 09, 2018 3: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 `?

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

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

Post by jvierra » Fri Nov 09, 2018 6:49 am

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: 58
Joined: Fri Aug 18, 2017 4:36 am

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

Post by lontru » Fri Nov 09, 2018 2:53 pm

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

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

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

Post by jvierra » Fri Nov 09, 2018 5:00 pm

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.

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

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

Post by jvierra » Fri Nov 09, 2018 6:50 pm

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.

Locked