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

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

Post by lontru » Thu Nov 15, 2018 2:51 pm

Image

how come this doesnt work?

Code: Select all

function Load-ListView ($collection)
{
	$listview1.Items.Clear()
	
	foreach ($i in $collection)
		{
			# add a list item
		$item = New-Object System.Windows.Forms.ListViewItem
		$item.Text = $i.key
		$item.Name = 'Property'
		
		# add subitems
		$subitem = New-Object System.Windows.Forms.ListViewItem+ListViewSubItem
		$subitem.Text = $i.value
		$subitem.Name = 'Value'
		$item.SubItems.Add($subitem)
		$listview1.Items.Add($item)
		}		
	}
}

and this does

Code: Select all

function Load-ListView ($collection)
{
	$listview1.Items.Clear()
	
	$collection.GetEnumerator() | ForEach-Object{
		
		# add a list item
		$item = New-Object System.Windows.Forms.ListViewItem
		$item.Text = $_.key
		$item.Name = 'Property'
		
		# add subitems
		$subitem = New-Object System.Windows.Forms.ListViewItem+ListViewSubItem
		$subitem.Text = $_.value
		$subitem.Name = 'Value'
		$item.SubItems.Add($subitem)
		
		$listview1.Items.Add($item)
	}
}
how do i group group the item in a smart way?
Attachments
get-sysinfo.psf
(31.11 KiB) Downloaded 27 times

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

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

Post by jvierra » Thu Nov 15, 2018 3:07 pm

You have not added any items to a group.

Please look carefully at the example I posted earlier. It shows the correct way to create items and subitems and how to add them to groups.

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

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

Post by lontru » Thu Nov 15, 2018 3:31 pm

I came up with this but it isnt very smart i think?

Code: Select all

		$General = 'Computer Name', 'Last Restart'
		$Build_Info = 'Manufactorer', 'Model', 'Name'
		$Operating_System = 'OS', 'Service Pack', 'Version', 'OS Architecture', 'Install Date'
		$Hardware = 'CPU','RAM', 'HD[1]', 'HD[2]', 'HD[3]', 'HD[4]', 'HD[5]', 'HD[6]', 'HD[7]', 'HD[8]', 'HD[9]', 'Monitor(s)'
		$Network_Adapters = 'Description[1]', 'IPAddress[1]', 'MACAddress[1]', 'Description[2]', 'IPAddress[2]', 'MACAddress[2]', 'Description[3]', 'IPAddress[3]', 'MACAddress[3]' , 'Description[4]', 'IPAddress[4]', 'MACAddress[4]'
		
		if ($General -match "$($_.key)")
		{
			$item.Group = $listview1.Groups['General']
		}
		
		if ($Build_Info -match "$($_.key)") {
			$item.Group = $listview1.Groups['Build Info']
		}
		
		if ($Operating_System -match "$($_.key)")
		{
			$item.Group = $listview1.Groups['Operating System']
		}
		
		if ($Hardware -contains "$($_.key)")
		{
			$item.Group = $listview1.Groups['Hardware']
		}
		
		if ($Network_Adapters -contains "$($_.key)")
		{
			$item.Group = $listview1.Groups['Network Adapters']
		}
Attachments
get-sysinfo_Hashtable.psf
(29.46 KiB) Downloaded 26 times

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

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

Post by lontru » Thu Nov 15, 2018 3:43 pm

Code: Select all

		$General = 'Computer Name', 'Last Restart'
		$Build_Info = 'Manufactorer', 'Model', 'Name'
		$Operating_System = 'OS', 'Service Pack', 'Version', 'OS Architecture', 'Install Date'
		$Hardware = 'CPU','RAM', 'Monitor(s)'
		
		if ($General -match "$($_.key)")
		{
			$item.Group = $listview1.Groups['General']
		}
		if ($Build_Info -match "$($_.key)") {
			$item.Group = $listview1.Groups['Build Info']
		}
		if ($Operating_System -match "$($_.key)")
		{
			$item.Group = $listview1.Groups['Operating System']
		}
		if (($Hardware -contains "$($_.key)") -or "$($_.key)" -like "HD*")
		{
			$item.Group = $listview1.Groups['Hardware']
		}
		if (($($_.key) -like "Description*") -or ($($_.key) -like "IPAddress*") -or ($($_.key) -like "MACAddress*"))
		{
			$item.Group = $listview1.Groups['Network Adapters']
		}
Now it can group more HDD and NetworkAdapters but is it the best way to do it.

Saw in another tread that gridview was recommend? but can you group items there like listview?

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

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

Post by jvierra » Thu Nov 15, 2018 3:52 pm

You have to create a category in your collection before assigning it to items and groups. This is best an easiest done when creating the collection.

Gridview controls cannot do grouping.

All of your issues require advanced programming and program design skills. Perhaps you should take a simpler approach.

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

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

Post by lontru » Fri Nov 16, 2018 12:13 am

You have to create a category in your collection before assigning it to items and groups. This is best an easiest done when creating the collection.

Not following you my hashtable can only hold property and value`? how do you add a group ?

Could you make a mini sample

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

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

Post by jvierra » Fri Nov 16, 2018 2:01 am

You cannot easily use a hash to create a ListView. Why ae you using a hash. Convert it into an object collection and add a property that identifies the group.

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

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

Post by jvierra » Fri Nov 16, 2018 2:33 am

Everywhere you have one of these:

Code: Select all

$sysinfo.Add("Computer Name", "$($sysCompp.PSComputerName)")
You need to do this:

Code: Select all

[pscustomobject]@{
    Name = 'Computer Name'
    Value = $sysCompp.PSComputerName
    Group = 'Operating System' # or what ever group you want it in.
}
In the end a ListView is not designed to do what you are trying to do. It will work but it is a stretch.

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

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

Post by jvierra » Fri Nov 16, 2018 2:40 am

Note also that the PSF you posted above des show in groups. Your method does work so I am at a loss to understand what the issue is. That is about the only two ways to create a grouped output.

A TreeView can also display in groups.

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

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

Post by jvierra » Fri Nov 16, 2018 3:40 am

Here is a cleaner way to access groups using your code:

Code: Select all

$groups = @{
    'General' = 'Computer Name,Last Restart'
    'Build Info' = 'Manufactorer,Model,Name'
    'Operating System' = 'OS,Service Pack,Version,OS Architecture,Install Date'
    'Hardware' = 'CPU,RAM,HD[1],HD[2],HD[3],HD[4],HD[5],HD[6],HD[7],HD[8],HD[9],Monitor(s)'
    'Network Adapters' = 'Description[1],IPAddress[1],MACAddress[1],Description[2],IPAddress[2],MACAddress[2],Description[3],IPAddress[3],MACAddress[3],Description[4],IPAddress[4],MACAddress[4]'
}

function Load-ListView ($collection){
	$listview1.Items.Clear()
	
	$collection.GetEnumerator() | ForEach-Object{
		
		# add a list item
		$item = New-Object System.Windows.Forms.ListViewItem
		$item.Text = $_.key
		$item.Name = 'Property'
		
		# add subitems
		$subitem = New-Object System.Windows.Forms.ListViewItem+ListViewSubItem
		$subitem.Text = $_.value
		$subitem.Name = 'Value'
		$item.SubItems.Add($subitem)
		
               # add group to item
               $key =  $_.Key
               $groupname = ($groups.GetEnumerator() |?{$_.Value -match $key}).Name
               $item.Group = $listview1.Groups[$groupname]
	       $listview1.Items.Add($item)
	}	
}
You could also place the list for each group in the group tag property and enumerate $listview1.Groups to find the correct group.

Code: Select all

$key =  $_.Key
$item.Group = $listview1.Groups | ?{$_.Tag -match $key }

Locked