Windows.Forms.ListView

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 7 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
hrlarsen
Posts: 31
Last visit: Sun Dec 18, 2022 1:47 pm

Windows.Forms.ListView

Post by hrlarsen »

Windows.Forms.ListView

I have a button to click that are working fine.
But how do I check "1 specific ListView Item".
If it is Checked in the checkbox, True / False before going forward.
--------------------------------------------------------------
# Add list items
$ListViewItem = New-Object System.Windows.Forms.ListViewItem("Item 1")
$ListView.Items.Add($ListViewItem)
-------------------------------------------------------------

-------------------------------------------------------------
#Button
$ButtonCheckedItem.add_click({
if($ListViewItem.CheckedItems.Count[1])
{
#perform action test
start-process "https://www.google.com"
}
$Form.Controls.Add($ButtonCheckedItem)
})
------------------------------------------------------------
What do I need to chance?
And please dont link to online msn I have looked there.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Windows.Forms.ListView

Post by jvierra »

You need to enumerate the ListView items collection and test each check or use the "ItemChecked" event to perform an action when a checkbox is checked:

For information on how to script a ListView see: https://info.sapien.com/index.php/guis/ ... ew-control
hrlarsen
Posts: 31
Last visit: Sun Dec 18, 2022 1:47 pm

Re: Windows.Forms.ListView

Post by hrlarsen »

Can I number every item whit number 1-2-3 "ItemChecked"
Is possible so something like this can work?

#Item 1
if ($ListViewItem.ItemChecked($1))
{
#test me out if item 1 checkbox is clicked.
start-process "https://www.google.dk"
}

And give this the number 1
# Add list items
$ListViewItem = New-Object System.Windows.Forms.ListViewItem("Item 1")
$ListView.Items.Add($ListViewItem)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Windows.Forms.ListView

Post by jvierra »

Code: Select all

if($listview1.Items['Item 1'].Checked) { 
     start-process https://www.google.dk
}
hrlarsen
Posts: 31
Last visit: Sun Dec 18, 2022 1:47 pm

Re: Windows.Forms.ListView

Post by hrlarsen »

I have tried

Code: Select all

[b]   if($ListView.Items['Item 1'].Checked) { 
        start-process https://www.google.dk
   }
[/b]
But nothing happens when I run it, and click the first item 1.
and click the button. I dont get any error code to check either.

------------------------------------------------------------------------------------


I have then tried this

Code: Select all

 if($ListViewItem1.Items['Item 1'].Checked) { 
        start-process https://www.google.dk
   }
Now I get and error code:
Cannot index into a null array.


------------------------------------------------------------------------------------

I have then after tried this:

Code: Select all

    if ($ListViewItem1.Checked)
	{ 
        #test me
        start-process "https://www.google.dk"	
}
This work for checking 1 item $ListViewItem1 that i Renamed from $ListViewItem as a test.

But that must be the wrong way to do this?
I dont thing it is the right way, there must be a way to check via a string $1 = or via text ("Item 1")

So maybe this need to be transformet or changed to resolved this.
# Add list items
$ListViewItem1 = New-Object System.Windows.Forms.ListViewItem("Item 1")
$ListView.Items.Add($ListViewItem1)
$Form.Controls.Add($ListView)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Windows.Forms.ListView

Post by jvierra »

What is the name of the item. If you didn't set a name then it will be unusable.
hrlarsen
Posts: 31
Last visit: Sun Dec 18, 2022 1:47 pm

Re: Windows.Forms.ListView

Post by hrlarsen »

The name is $1 for Item
# Add list items

Code: Select all

$ListViewItem = New-Object System.Windows.Forms.ListViewItem('Item 1')
$ListView.Items.Add($ListViewItem)
$ListViewItem.Name = '$1'
$Form.Controls.Add($ListView)
Last edited by hrlarsen on Mon Aug 06, 2018 2:28 pm, edited 1 time in total.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Windows.Forms.ListView

Post by jvierra »

What is $1? It is a variable. Look at the name property on the tab to see what the real name is.
hrlarsen
Posts: 31
Last visit: Sun Dec 18, 2022 1:47 pm

Re: Windows.Forms.ListView

Post by hrlarsen »

As standard (name) is $listView.Name = "listView1"
But I have renamed it to = "$1"

What variable code am I missing.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Windows.Forms.ListView

Post by jvierra »

"$1" is not a name. We don't use "$" in names as it mixes them up with variables. Names need to strings that identify things in a meaningful way. "$1" is not meaningful and will result in an empty name.
This topic is 5 years and 7 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