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: 73
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Fri Aug 18, 2017 4:36 am

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

Post by lontru »

First off i would like to Thank you for the time you put in this. Helping others in need.

I tryed your code but it return this error?

Code: Select all

ERROR: Exception calling "Add" with "2" argument(s): "Item has already been added. Key in dictionary: ''  Key being added: ''"
MainForm.psf (345, 20): ERROR: At Line: 345 char: 20
ERROR: +             $_.SubItems | %{ $hash.Add($_.Name, $_.Text) }
ERROR: +                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
ERROR:     + FullyQualifiedErrorId : ArgumentException

jvierra
Posts: 14335
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra »

Either you have duplicate item names or you have not named the items.

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

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

Post by lontru »

Image

All the property need to be uniq?

Any simple solution?

Code: Select all


	$sysNAC = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $ComputerName -Filter "IPEnabled='True'"
	## Network Adapters
	$sysNAC | %{
		Add-ListViewItem -ListView $listview_sysinfo -Items "Description" -SubItems "$($_.Description)" -Group "Network Adapters"
		Add-ListViewItem -ListView $listview_sysinfo -Items "IPAddress" -SubItems "$($_.IPAddress)" -Group "Network Adapters"
		Add-ListViewItem -ListView $listview_sysinfo -Items "MACAddress" -SubItems "$($_.MACAddress)" -Group "Network Adapters"
	}

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

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

Post by lontru »

Code: Select all

	## Network Adapters
	$sysNAC | %{
		$NAC++
		Add-ListViewItem -ListView $listview_sysinfo -Items "Description[$NAC]" -SubItems "$($_.Description)" -Group "Network Adapters"
		Add-ListViewItem -ListView $listview_sysinfo -Items "IPAddress[$NAC]" -SubItems "$($_.IPAddress)" -Group "Network Adapters"
		Add-ListViewItem -ListView $listview_sysinfo -Items "MACAddress[$NAC]" -SubItems "$($_.MACAddress)" -Group "Network Adapters"
	}
Above code would solve the name uniqueness

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

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

Post by lontru »

have attactted the mainform.psf

Code: Select all

ERROR: Exception calling "Add" with "2" argument(s): "Item has already been added. Key in dictionary: ''  Key being added: ''"
Attachments
MainForm.psf
(29.06 KiB) Downloaded 30 times

jvierra
Posts: 14335
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra »

You have to build the list items correctly . See the attached example.
Attachments
Demo-ListViewIcons.psf
(25.7 KiB) Downloaded 39 times

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

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

Post by lontru »

Code: Select all

function Load-ListView{
    
    $listview1.Items.Clear()
    Get-ChildItem $textboxFolder.Text | 
    	ForEach-Object{
        
            # add a list item
    		$item = New-Object System.Windows.Forms.ListViewItem
    		$item.Text = $_.Name
    		$item.Name = 'FileName'
            
            # add subitems
    		$subitem = New-Object System.Windows.Forms.ListViewItem+ListViewSubItem
    		$subitem.Name = 'Length'
    		$subitem.Text = $_.Length
    		$item.SubItems.Add($subitem)

    		$subitem = New-Object System.Windows.Forms.ListViewItem+ListViewSubItem
    		$subitem.Name = 'Fullname'
    		$subitem.Text = $_.Fullname
    		$item.SubItems.Add($subitem)

    		if ($_.PsIsContainer) {
                $item.ImageKey = 'Folder'
                $item.Group = $listview1.Groups['Folders']
            } else {
                $item.ImageKey = 'File'
                $item.Group = $listview1.Groups['Files']
            }
    		$listview1.Items.Add($item)
    	}
    $listview1.Enabled = $true
}
So i need to build a custom psobject with all the data and do a foreach on it to build up the listview.
How do i decide the order? grouping

Image

jvierra
Posts: 14335
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra »

The grouping is explicitly set in the code. You assign each item to a group.

If you run the form in the debugger and set breakpoints on each event you will see how it works.
If you use items that are all different structures it cannot be exported to a CSV.

You need to understand how a ListView is designed to work. After you have figured it out you will see what it is capable of and how to manage it.

Here is a place to start learning about what a ListView is and how to use it:

https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/listview-control-overview-windows-forms

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

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

Post by lontru »

following your demo i created this but i don't get the same result?

all data is on one line?

What have gone wrong?? I created a hash table with all the value and did a foreach on that

Image
Attachments
get-sysinfo.psf
(27.29 KiB) Downloaded 31 times

jvierra
Posts: 14335
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra »

You did not do what I posted in the example. PLease run my example and step through with the debugger until you understand what it is doing.

Locked