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

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

Post by lontru » Mon Nov 12, 2018 3:54 am

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

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

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

Post by jvierra » Mon Nov 12, 2018 4:00 am

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

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

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

Post by lontru » Mon Nov 12, 2018 4:06 am

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

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

Post by lontru » Mon Nov 12, 2018 4:12 am

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

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

Post by lontru » Mon Nov 12, 2018 4:17 am

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 4 times

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

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

Post by jvierra » Mon Nov 12, 2018 4:42 am

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

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

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

Post by lontru » Mon Nov 12, 2018 6:33 am

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

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

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

Post by jvierra » Mon Nov 12, 2018 6:43 am

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

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

Post by lontru » Wed Nov 14, 2018 2:16 pm

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 5 times

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

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

Post by jvierra » Wed Nov 14, 2018 2:54 pm

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.

Post Reply