{"id":3993,"date":"2012-04-05T12:31:00","date_gmt":"2012-04-05T20:31:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=3993"},"modified":"2012-04-11T09:06:02","modified_gmt":"2012-04-11T17:06:02","slug":"spotlight-on-the-listview-control-part-2","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2012\/04\/05\/spotlight-on-the-listview-control-part-2\/","title":{"rendered":"Spotlight on the ListView Control &#8211; Part 2"},"content":{"rendered":"<p><em>The \u201cSpotlight on Controls\u201d series focuses on a single WinForms control in PrimalForms 2011 , details the important Properties, Methods, and Events of the control and demonstrates how to utilize the control. Most of the information about the controls is still applicable to previous versions of PrimalForms.<\/em><\/p>\n<p>In <a href=\"http:\/\/www.sapien.com\/blog\/2012\/04\/04\/spotlight-on-the-listview-control-part-1\/\">Part 1<\/a> of the Spotlight on the ListView Control we discussed a number of important properties. Here, in Part 2, we complete our look at the control&#8217;s properties and we will also cover some of the more important methods and events.<\/p>\n<p><strong><span style=\"font-size: small\"><font size=\"3\">Important Properties Continued:<\/font><\/span><\/strong><\/p>\n<p><strong>Items<\/strong><\/p>\n<p>This property contains a collection of all the items in the ListView. All items in a ListView are represented by a ListViewItem object:<\/p>\n<p><strong>ListViewItem<\/strong> [System.Windows.Forms.ListViewItem]<\/p>\n<blockquote>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\" width=\"504\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"120\"><strong>Property<\/strong><\/td>\n<td valign=\"top\" width=\"382\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"120\">Checked<\/td>\n<td valign=\"top\" width=\"382\">This property indicates whether the item is checked.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"120\">Text<\/td>\n<td valign=\"top\" width=\"382\">This property contains the displayed text of the item.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"120\">ToolTipText<\/td>\n<td valign=\"top\" width=\"382\">This property contains the text shown when the mouse pointer rests on the ListViewItem.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"120\">Group<\/td>\n<td valign=\"top\" width=\"382\">This property points to the group to which the item is assigned.<br \/>Set this property to any existing Group. All items with an unsigned Group will be show in the \u201cDefault\u201d Group.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"120\">ImageIndex<\/td>\n<td valign=\"top\" width=\"382\">This property indicates the index of the image that is displayed for the item.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"120\">StateImageIndex<\/td>\n<td valign=\"top\" width=\"382\">This property indicates the index of the state image (an image such as a selected or cleared check box that indicates the state of the item) that is displayed for the item.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"120\">SubItems<\/td>\n<td valign=\"top\" width=\"382\">This property is a collection containing all subitems of the item.<br \/>Compatible Views: <em>Details<\/em>, <em>Tile<br \/><\/em><br \/>In the <em>Details<\/em> view the Subitems will be displayed only if Columns are defined.<br \/><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2012\/04\/SubItem-Details4.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"SubItem-Details4\" border=\"0\" alt=\"SubItem-Details4\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2012\/04\/SubItem-Details4_thumb.png\" width=\"203\" height=\"43\"><\/a><\/p>\n<p>In the <em>Tile<\/em> view only the first SubItem will be displayed (below the Item\u2019s text).<br \/><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"SubItem Tile\" border=\"0\" alt=\"SubItem Tile\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2012\/04\/SubItem-Tile.png\" width=\"133\" height=\"35\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/blockquote>\n<p><strong><em>Adding Items to the ListView:<\/em><\/strong><\/p>\n<p>You can access the Items Collection Editor via the Designer:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2012\/04\/Item-Collection-Editor-Via-Designer_.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Item-Collection-Editor-Via-Designer_\" border=\"0\" alt=\"Item-Collection-Editor-Via-Designer_\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2012\/04\/Item-Collection-Editor-Via-Designer__thumb.png\" width=\"244\" height=\"200\"><\/a><\/p>\n<p>Or via the Property Pane:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2012\/04\/Item-Collection-Editor-via-Property-.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Item-Collection-Editor-via-Property-\" border=\"0\" alt=\"Item-Collection-Editor-via-Property-\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2012\/04\/Item-Collection-Editor-via-Property-_thumb.png\" width=\"275\" height=\"117\"><\/a><\/p>\n<p>The ListViewItem Collection Editor allows you to add \/ remove Item as well set their properties:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2012\/04\/ListViewItem-Collection-Editor_thumb.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"ListViewItem-Collection-Editor_thumb\" border=\"0\" alt=\"ListViewItem-Collection-Editor_thumb\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2012\/04\/ListViewItem-Collection-Editor_thumb_thumb.png\" width=\"244\" height=\"156\"><\/a><\/p>\n<p>Via the Script Editor:<\/p>\n<p><span style=\"font-size: x-small\"><font size=\"2\"><span style=\"color: #0000ff\">[ListViewItem]<\/span> <span style=\"color: #4bacc6\">Add <\/span>(<span style=\"color: #0000ff\">[<\/span><span style=\"color: #0000ff\">ListViewItem] <\/span><span style=\"color: #333333\">value<\/span>)<br \/>Adds an existing ListViewItem to the collection.<\/font><\/span><\/p>\n<p><span style=\"font-size: x-small\"><font size=\"2\"><img decoding=\"async\" title=\"Public method\" alt=\"Public method\" src=\"http:\/\/i.msdn.microsoft.com\/Hash\/030c41d9079671d09a62d8e2c1db6973.gif\"><br \/><span style=\"color: #0000ff\">[ListViewItem] <\/span><span style=\"color: #4bacc6\">Add <\/span>(<span style=\"color: #0000ff\">[<\/span><span style=\"color: #0000ff\">String] <\/span><span style=\"color: #000000\">text<\/span>)<br \/>Creates an item with the specified text and adds it to the collection.<\/font><\/span><\/p>\n<p><span style=\"font-size: x-small\"><font size=\"2\"><span style=\"color: #0000ff\">[ListViewItem] <\/span><span style=\"color: #4bacc6\">Add <\/span>(<span style=\"color: #0000ff\">[<\/span><span style=\"color: #0000ff\">String] <span style=\"color: #000000\">text<\/span><\/span>, <span style=\"color: #0000ff\">[<\/span><span style=\"color: #0000ff\">Int] <\/span><span style=\"color: #000000\">imageIndex<\/span>)<br \/>Creates an item with the specified text and image and adds it to the collection.<\/font><\/span><\/p>\n<p><span style=\"font-size: x-small\"><font size=\"2\"><span style=\"color: #0000ff\">[ListViewItem] <\/span><span style=\"color: #4bacc6\">Add <\/span>(<span style=\"color: #0000ff\">[<\/span><span style=\"color: #0000ff\">String] <span style=\"color: #000000\">text<\/span><\/span>, <span style=\"color: #0000ff\">[<\/span><span style=\"color: #0000ff\">String] <\/span><span style=\"color: #000000\">imageKey<\/span>)<br \/>Creates an item with the specified text and image and adds it to the collection.<\/font><\/span><\/p>\n<p><span style=\"font-size: x-small\"><font size=\"2\"><span style=\"color: #0000ff\">[ListViewItem] <\/span><span style=\"color: #4bacc6\">Add <\/span>(<span style=\"color: #0000ff\">[<\/span><span style=\"color: #0000ff\">String] k<span style=\"color: #000000\">ey<\/span><\/span>, <span style=\"color: #0000ff\">[<\/span><span style=\"color: #0000ff\">String] <span style=\"color: #000000\">text<\/span>, <span style=\"color: #0000ff\">[<\/span><span style=\"color: #0000ff\">Int] <span style=\"color: #000000\">imageIndex<\/span><\/span><\/span>)<br \/>Creates an item with the specified key, text, and image and adds an item to the collection.<\/font><\/span><\/p>\n<p><span style=\"font-size: x-small\"><font size=\"2\"><span style=\"color: #0000ff\">[ListViewItem] <\/span><span style=\"color: #4bacc6\">Add <\/span>(<span style=\"color: #0000ff\">[<\/span><span style=\"color: #0000ff\">String] k<span style=\"color: #000000\">ey<\/span><\/span>, <span style=\"color: #0000ff\">[<\/span><span style=\"color: #0000ff\">String] <span style=\"color: #000000\">text<\/span>, <span style=\"color: #0000ff\">[<\/span><span style=\"color: #0000ff\">String] <span style=\"color: #000000\">imageKey<\/span><\/span><\/span>)<br \/>Creates an item with the specified key, text, and image and adds an item to the collection.<\/font><\/span><\/p>\n<p>Example Use:<\/p>\n<p><span style=\"color: #8b0000\">$listitem <\/span><span style=\"color: #0000ff\">= <\/span><span style=\"color: #8b0000\">$listview1<\/span><span style=\"color: #000000\">.Items.Add(<\/span><span style=\"color: #ff0000\">&#8216;PrimalForms&#8217;<\/span><span style=\"color: #000000\"> , <\/span><span style=\"color: #000000\">2<\/span><span style=\"color: #000000\">)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: small\"><strong><em>Adding SubItems:<\/em><\/strong><\/span><\/p>\n<p>In the cases where you wish to add SubItems to the ListView you can add them using:<\/p>\n<p>Via the ListViewItem Collection Editor:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2012\/04\/SubItems-in-Collection-Editor4.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"SubItems-in-Collection-Editor4\" border=\"0\" alt=\"SubItems-in-Collection-Editor4\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2012\/04\/SubItems-in-Collection-Editor4_thumb.png\" width=\"310\" height=\"148\"><\/a><\/p>\n<p>By clicking on the SubItems property in the ListViewItem Collection Editor, you will be presented with a ListViewSubItem Collection Editor.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2012\/04\/ListViewSubItem-Collection-Editor_th.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"ListViewSubItem-Collection-Editor_th\" border=\"0\" alt=\"ListViewSubItem-Collection-Editor_th\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2012\/04\/ListViewSubItem-Collection-Editor_th_thumb.png\" width=\"244\" height=\"180\"><\/a><\/p>\n<p>The only important property you need to set for a ListViewSubItemis the Text property.<\/p>\n<p>Via the Script Editor:<\/p>\n<p>The SubItems property has the following methods to add ListViewSubItems:<\/p>\n<p><span style=\"font-size: x-small\"><font size=\"2\"><span style=\"color: #0000ff\">[ListViewSubItem]<\/span> <span style=\"color: #4bacc6\">Add <\/span>(<span style=\"color: #0000ff\">[<\/span><span style=\"color: #0000ff\">ListViewSubItem] <\/span><span style=\"color: #000000\">item<\/span>)<br \/>Adds an existing ListViewItem.ListViewSubItem to the collection.<\/font><\/span><\/p>\n<p><span style=\"font-size: x-small\"><font size=\"2\"><img decoding=\"async\" title=\"Public method\" alt=\"Public method\" src=\"http:\/\/i.msdn.microsoft.com\/Hash\/030c41d9079671d09a62d8e2c1db6973.gif\"><br \/><span style=\"color: #0000ff\">[ListViewSubItem] <\/span><span style=\"color: #4bacc6\">Add <\/span>(<span style=\"color: #0000ff\">[<\/span><span style=\"color: #0000ff\">String] <\/span><span style=\"color: #000000\">text<\/span>)<br \/>Adds a subitem to the collection with specified text.<\/font><\/span><\/p>\n<p><span style=\"font-size: x-small\"><font size=\"2\"><span style=\"color: #0000ff\">[ListViewSubItem] <\/span><span style=\"color: #4bacc6\">Add <\/span>(<span style=\"color: #0000ff\">[<\/span><span style=\"color: #0000ff\">String] <span style=\"color: #000000\">text<\/span><\/span>, <span style=\"color: #0000ff\">[Color<\/span><span style=\"color: #0000ff\">] <span style=\"color: #000000\">foreColor<\/span>, <span style=\"color: #0000ff\">[Color<\/span><span style=\"color: #0000ff\">] <span style=\"color: #000000\">backColor,<\/span> [Font] <\/span><\/span><span style=\"color: #000000\">font<\/span>)<br \/>Adds a subitem to the collection with specified text, foreground color, background color, and font settings.<\/font><\/span><\/p>\n<p>Example Use:<\/p>\n<pre><span style=\"color: #8b0000\">$subItem <\/span><span style=\"color: #0000ff\">= <\/span><span style=\"color: #8b0000\">$listitem<\/span><span style=\"color: #000000\">.SubItems.Add(<\/span><span style=\"color: #ff0000\">'Installed'<\/span><span style=\"color: #000000\">)<\/span><\/pre>\n<p><strong><\/strong><\/p>\n<pre><\/pre>\n<p><span style=\"font-size: small\"><strong><font size=\"3\">Important Methods:<\/font><\/strong><\/span><\/p>\n<p><strong>Clear<\/strong><\/p>\n<p>This method removes all items and columns from the ListView.<\/p>\n<pre><span style=\"color: #8b0000\">$listview1<\/span><span style=\"color: #000000\">.Clear()<\/span><\/pre>\n<p><strong>BeginUpdate<\/strong><\/p>\n<p>This method prevents the control from drawing until the EndUpdate method is called. Use this method when you are updating or adding a large amount of items.<\/p>\n<pre><span style=\"color: #8b0000\">$listview1<\/span><span style=\"color: #000000\">.BeginUpdate()<\/span><\/pre>\n<p><strong>EndUpdate<\/strong><\/p>\n<p>This method resumes drawing of the list view control after drawing is suspended by the BeginUpdate method.<\/p>\n<pre><span style=\"color: #8b0000\">$listview1<\/span><span style=\"color: #000000\">.EndUpdate()<\/span><\/pre>\n<p>Example use of the BeginUpdate and EndUpdate:<\/p>\n<pre><span style=\"color: #8b0000\">$listview1<\/span><span style=\"color: #000000\">.BeginUpdate() \n<\/span><span style=\"color: #8b0000\">$listview1<\/span><span style=\"color: #000000\">.Items.Clear(); \n<\/span><span style=\"color: #0000ff\">foreach<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$itemin$array<\/span><span style=\"color: #000000\">) \n{ \n    [void]<\/span><span style=\"color: #8b0000\">$listview1<\/span><span style=\"color: #000000\">.Items.Add(<\/span><span style=\"color: #8b0000\">$item<\/span><span style=\"color: #000000\">.Text)\n} \n<\/span><span style=\"color: #8b0000\">$listview1<\/span><span style=\"color: #000000\">.EndUpdate();<\/span><\/pre>\n<p><strong><\/strong><\/p>\n<p><strong><span style=\"font-size: small\"><\/span><\/strong>&nbsp;<\/p>\n<p><strong><span style=\"font-size: small\"><font size=\"3\">Important Events:<\/font><\/span><\/strong><\/p>\n<p><strong>AfterLabelEdit<\/strong><\/p>\n<p>This event occurs when the label for an item has been edited by the user. Label editing must be enabled for this event to be called (See LabelEdit property).<\/p>\n<p>AfterLabelEdit event uses the following argument, which is accessible via the <span style=\"color: #8b0000\">$_<\/span> variable:<\/p>\n<p><strong>[System.Windows.Forms.LabelEditEventHandler]<\/strong><\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\" width=\"452\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"80\"><strong>Properties<\/strong><\/td>\n<td valign=\"top\" width=\"370\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"80\">CancelEdit<\/td>\n<td valign=\"top\" width=\"370\">Gets or sets a value indicating whether the edit has been canceled.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"80\">Label<\/td>\n<td valign=\"top\" width=\"370\">Gets the new text assigned to the label of the ListViewItem.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"80\">Item<\/td>\n<td valign=\"top\" width=\"370\">Gets the zero-based index of the ListViewItem containing the label to edit.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Use this event to react to name changes. For example if the represents a file, you can use this event as a trigger for renaming the file. You should also perform any validation and reject the name change if there is a failure.<\/p>\n<pre><span style=\"color: #8b0000\">$listview1_AfterLabelEdit<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">[System.Windows.Forms.LabelEditEventHandler]{\n    <\/span><span style=\"color: #008000\">#Event Argument: $_ = [System.Windows.Forms.LabelEditEventArgs]<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000ff\">-not<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #008080\">Validate-FileName<\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.Label)) \n    { \n        <\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.CancelEdit <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\">\n    } \n    <\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #000000\"> \n    { \n        <\/span><span style=\"color: #008000\">#Rename the file <\/span><span style=\"color: #000000\">\n    } \n}<\/span><\/pre>\n<p><strong>BeforeLabelEdit<\/strong><\/p>\n<p>This event occurs when the user starts editing the label of an item.<\/p>\n<p>This event uses the [System.Windows.Forms.LabelEditEventHandler] argument. See AfterLabelEdit for more information about this argument.<\/p>\n<p>Use this event to prevent users from renaming specific items.<\/p>\n<pre><span style=\"color: #8b0000\">$listview1_BeforeLabelEdit<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">[System.Windows.Forms.LabelEditEventHandler]{ \n    <\/span><span style=\"color: #008000\">#Event Argument: $_ = [System.Windows.Forms.LabelEditEventArgs]<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000ff\">-not<\/span><span style=\"color: #8b0000\">$listview1<\/span><span style=\"color: #000000\">.Items[<\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.Item].Checked) \n    { \n        <\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.CancelEdit <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\"> \n    } \n}<\/span><\/pre>\n<p><strong>ColumnClick<\/strong><\/p>\n<p>This event occurs when the user clicks a column header within the list view control. Typically you will use this event to sort the list by the selected column.<\/p>\n<p>ColumnClick event uses the following argument, which is accessible via the <span style=\"color: #8b0000\">$_<\/span> variable:<\/p>\n<p><strong>[System.Windows.Forms.ColumnClickEventArgs]<\/strong><\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\" width=\"452\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"80\"><strong>Properties<\/strong><\/td>\n<td valign=\"top\" width=\"370\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"80\">Column<\/td>\n<td valign=\"top\" width=\"370\">Gets the zero-based index of the column that is clicked.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><em><strong><\/strong><\/em><\/p>\n<pre><span style=\"color: #8b0000\">$listview1_ColumnClick<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">[System.Windows.Forms.ColumnClickEventHandler]{ \n    <\/span><span style=\"color: #008000\">#Event Argument: $_ = [System.Windows.Forms.ColumnClickEventArgs]<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #0000ff; font-weight: bold\">Write-Host<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #ff0000\">\"Column '{0}' was clicked\"<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-f<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$this<\/span><span style=\"color: #000000\">.Columns[<\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.Column].Text)\n}<\/span><\/pre>\n<p>See Part 3&nbsp; for how to sort columns.<\/p>\n<p><strong>ItemActivate<\/strong><\/p>\n<p>This event occurs when an item is activated. One or more items can be activated by the user, depending on the Activate property. Note: Activating an item is not the same as selecting an item. For example, you may want to display a webpage when the user activates an item in the ListView. Use the SelectedItems or SelectedIndices property to referenced the activated item.<\/p>\n<pre><span style=\"color: #8b0000\">$listview1_ItemActivate<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">{ \n    <\/span><span style=\"color: #008000\">#TODO: Place custom script here<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #0000ff; font-weight: bold\">Write-Host<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">\"Activate Item\"<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$listview1<\/span><span style=\"color: #000000\">.SelectedItems[<\/span><span style=\"color: #000000\">0<\/span><span style=\"color: #000000\">].Text \n}<\/span><\/pre>\n<p><strong>ItemChecked<\/strong><\/p>\n<p>This event occurs when the checked property of a ListView item changes.<\/p>\n<p>ItemChecked event uses the following argument, which is accessible via the <span style=\"color: #8b0000\">$_<\/span> variable:<\/p>\n<p><strong>[System.Windows.Forms.ItemCheckedEventArgs]<\/strong><\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\" width=\"452\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"80\"><strong>Properties<\/strong><\/td>\n<td valign=\"top\" width=\"370\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"80\">Item<\/td>\n<td valign=\"top\" width=\"370\">Gets the ListViewItem whose checked state is changing.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre><span style=\"color: #8b0000\">$listview1_ItemChecked<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">[System.Windows.Forms.ItemCheckedEventHandler]{\n    <\/span><span style=\"color: #008000\">#Event Argument: $_ = [System.Windows.Forms.ItemCheckedEventArgs]<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.Item.Checked)\n    { \n        <\/span><span style=\"color: #0000ff; font-weight: bold\">Write-Host<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">\"Checked Item: $($_.Item.Text)\"<\/span><span style=\"color: #000000\"> \n    } \n    <\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #000000\"> \n    { \n        <\/span><span style=\"color: #0000ff; font-weight: bold\">Write-Host<\/span><span style=\"color: #ff0000\">\"Unchecked Item: $($_.Item.Text)\"<\/span><span style=\"color: #000000\"> \n    } \n}<\/span><\/pre>\n<p><strong>SelectedIndexChanged<\/strong><\/p>\n<p>This event occurs when the SelectedIndices collection changes. Use the event when you want to react to a change in the selection.<\/p>\n<pre><span style=\"color: #8b0000\">$listview1_SelectedIndexChanged<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">{ \n    <\/span><span style=\"color: #0000ff; font-weight: bold\">Write-Host<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">\"Selection Changed\"<\/span><span style=\"color: #000000\"> \n}<\/span><\/pre>\n<p><strong><\/strong><\/p>\n<p><strong>ItemSelectionChanged<\/strong><\/p>\n<p>This event occurs when the selection state of an item changes. Use this event instead of the SelectedIndexChanged when you want to know the which item was selected or unselected.<\/p>\n<p>ItemSelectionChanged event uses the following argument, which is accessible via the <span style=\"color: #8b0000\">$_<\/span> variable:<\/p>\n<p><strong>[System.Windows.Forms.ListViewItemSelectionChangedEventArgs]<\/strong><\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\" width=\"452\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"80\"><strong>Properties<\/strong><\/td>\n<td valign=\"top\" width=\"370\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"80\">Item<\/td>\n<td valign=\"top\" width=\"370\">Gets the item whose selection state has changed.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"80\">IsSelected<\/td>\n<td valign=\"top\" width=\"370\">Gets a value indicating whether the item&#8217;s state has changed to selected.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"80\">ItemIndex<\/td>\n<td valign=\"top\" width=\"370\">Gets the index of the item whose selection state has changed.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre><span style=\"color: #8b0000\">$listview1_ItemSelectionChanged<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">[System.Windows.Forms.ListViewItemSelectionChangedEventHandler]{\n    <\/span><span style=\"color: #008000\">#Event Argument: $_ = [System.Windows.Forms.ListViewItemSelectionChangedEventArgs]<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.IsSelected)\n    {\n        <\/span><span style=\"color: #0000ff; font-weight: bold\">Write-Host<\/span><span style=\"color: #ff0000\">\"Selected: $($_.Item.Text)\"<\/span><span style=\"color: #000000\"> \n    } \n    <\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #000000\"> \n    { \n        <\/span><span style=\"color: #0000ff; font-weight: bold\">Write-Host<\/span><span style=\"color: #ff0000\">\"UnSelected: $($_.Item.Text)\"<\/span><span style=\"color: #000000\"> \n    } \n}<\/span><\/pre>\n<p>Next: <a href=\"http:\/\/www.sapien.com\/blog\/2012\/04\/11\/spotlight-on-the-listview-control-part-3\/\">Part 3 \u2013&nbsp; Sorting Columns and Helper Functions<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The \u201cSpotlight on Controls\u201d series focuses on a single WinForms control in PrimalForms 2011 , details the important Properties, Methods, and Events of the control and demonstrates how to utilize the control. Most of the information about the controls is still applicable to previous versions of PrimalForms. In Part 1 of the Spotlight on the [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[509,575,644,25],"tags":[641,699,28,633,327],"class_list":["post-3993","post","type-post","status-publish","format-standard","hentry","category-primalforms-software-news","category-primalforms-2011-software-news","category-spotlight-on-controls","category-windows-powershell","tag-controls","tag-listview","tag-powershell","tag-primalforms-2011","tag-winform"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3993","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=3993"}],"version-history":[{"count":7,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3993\/revisions"}],"predecessor-version":[{"id":4119,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3993\/revisions\/4119"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=3993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=3993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=3993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}