Use SelectedItems and CheckedItems at the same time

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 6 years and 11 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
User avatar
ramses147
Posts: 110
Last visit: Tue Dec 05, 2023 7:11 am
Been upvoted: 1 time

Use SelectedItems and CheckedItems at the same time

Post by ramses147 »

Hi guys !

I'd like to use both selection mode at the same time in a listview ( SelectedItems + CheckedItems ) and save the pcname inside a variable

For example i want to select pc-w7 and pc-w7x86 ( with selecteditems )

and w10-1 ( with checkeditems )

Image: //static.spiceworks.com/shared/post/0024/1507/a.jpg


i wrote this, but if i change the selection, there are multiple items with the same name

Powershell
function USER_PC_SELECT {

if ($listview2.CheckedItems.Count -gt 0 -or $listview2.SelectedItems.Count -gt 0){

$Global:SelectedComputer += $listview2.CheckedItems | foreach {$_.text}
$Global:SelectedComputer += $listview2.SelectedItems.text

Write-Host -ForegroundColor White "Selezionato/i computer: " -NoNewline ; Write-Host -ForegroundColor Green $Global:SelectedComputer
}
}
Attachments
a.jpg
a.jpg (8.86 KiB) Viewed 1304 times
User avatar
ramses147
Posts: 110
Last visit: Tue Dec 05, 2023 7:11 am
Been upvoted: 1 time

Re: Use SelectedItems and CheckedItems at the same time

Post by ramses147 »

This is could be the answer ?

There's a better method ?
  1. function USER_PC_SELECT {
  2.  
  3. if ($listview2.CheckedItems.Count -gt 0 -or $listview2.SelectedItems.Count -gt 0){
  4.  
  5. $Global:SelectedComputer = @()
  6.  
  7. foreach ($item in $listview2.CheckedItems.text){
  8. if ($Global:SelectedComputer -notcontains $item){
  9. $Global:SelectedComputer += $item
  10. }
  11. }
  12.  
  13. foreach ($item in $listview2.SelectedItems.text){
  14. if ($Global:SelectedComputer -notcontains $item){
  15. $Global:SelectedComputer += $item
  16. }
  17. }
  18.  
  19. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Use SelectedItems and CheckedItems at the same time

Post by jvierra »

Why would you want to try and use both methods. Just use on or the other. Why confuse the users.
This topic is 6 years and 11 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