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.
hrlarsen
Posts: 27
Joined: Tue Jul 24, 2018 8:36 pm

Windows.Forms.ListView

Post by hrlarsen » Mon Aug 06, 2018 3:32 am

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.

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

Re: Windows.Forms.ListView

Post by jvierra » Mon Aug 06, 2018 5:45 am

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: 27
Joined: Tue Jul 24, 2018 8:36 pm

Re: Windows.Forms.ListView

Post by hrlarsen » Mon Aug 06, 2018 6:25 am

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)

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

Re: Windows.Forms.ListView

Post by jvierra » Mon Aug 06, 2018 9:40 am

Code: Select all

if($listview1.Items['Item 1'].Checked) { 
     start-process https://www.google.dk
}

hrlarsen
Posts: 27
Joined: Tue Jul 24, 2018 8:36 pm

Re: Windows.Forms.ListView

Post by hrlarsen » Mon Aug 06, 2018 11:56 am

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)

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

Re: Windows.Forms.ListView

Post by jvierra » Mon Aug 06, 2018 12:22 pm

What is the name of the item. If you didn't set a name then it will be unusable.

hrlarsen
Posts: 27
Joined: Tue Jul 24, 2018 8:36 pm

Re: Windows.Forms.ListView

Post by hrlarsen » Mon Aug 06, 2018 2:24 pm

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.

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

Re: Windows.Forms.ListView

Post by jvierra » Mon Aug 06, 2018 2:28 pm

What is $1? It is a variable. Look at the name property on the tab to see what the real name is.

hrlarsen
Posts: 27
Joined: Tue Jul 24, 2018 8:36 pm

Re: Windows.Forms.ListView

Post by hrlarsen » Mon Aug 06, 2018 2:46 pm

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

What variable code am I missing.

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

Re: Windows.Forms.ListView

Post by jvierra » Mon Aug 06, 2018 3:14 pm

"$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.

Locked