Page 4 of 5

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

Posted: Thu Nov 15, 2018 2:51 pm
by lontru
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?

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

Posted: Thu Nov 15, 2018 3:07 pm
by jvierra
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.

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

Posted: Thu Nov 15, 2018 3:31 pm
by lontru
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']
		}

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

Posted: Thu Nov 15, 2018 3:43 pm
by lontru

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?

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

Posted: Thu Nov 15, 2018 3:52 pm
by jvierra
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.

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

Posted: Fri Nov 16, 2018 12:13 am
by lontru
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

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

Posted: Fri Nov 16, 2018 2:01 am
by jvierra
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.

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

Posted: Fri Nov 16, 2018 2:33 am
by jvierra
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.

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

Posted: Fri Nov 16, 2018 2:40 am
by jvierra
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.

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

Posted: Fri Nov 16, 2018 3:40 am
by jvierra
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 }