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.
This topic is 5 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
lontru
Posts: 103
Last visit: Sun Apr 14, 2024 11:13 pm
Post
by lontru » Mon Nov 12, 2018 2: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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
Post
by jvierra » Mon Nov 12, 2018 3:00 am
Either you have duplicate item names or you have not named the items.
lontru
Posts: 103
Last visit: Sun Apr 14, 2024 11:13 pm
Post
by lontru » Mon Nov 12, 2018 3:06 am
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"
}
lontru
Posts: 103
Last visit: Sun Apr 14, 2024 11:13 pm
Post
by lontru » Mon Nov 12, 2018 3: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
lontru
Posts: 103
Last visit: Sun Apr 14, 2024 11:13 pm
Post
by lontru » Mon Nov 12, 2018 3: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 108 times
lontru
Posts: 103
Last visit: Sun Apr 14, 2024 11:13 pm
Post
by lontru » Mon Nov 12, 2018 5: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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
Post
by jvierra » Mon Nov 12, 2018 5: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
lontru
Posts: 103
Last visit: Sun Apr 14, 2024 11:13 pm
Post
by lontru » Wed Nov 14, 2018 1: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
Attachments
get-sysinfo.psf
(27.29 KiB) Downloaded 112 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
Post
by jvierra » Wed Nov 14, 2018 1: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.
This topic is 5 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked