{"id":339,"date":"2010-07-09T15:15:27","date_gmt":"2010-07-09T15:15:27","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=339"},"modified":"2022-02-17T06:24:05","modified_gmt":"2022-02-17T06:24:05","slug":"using-list-view-control","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/list-view\/using-list-view-control","title":{"rendered":"Using List View Control"},"content":{"rendered":"<p>\nThe ListView control is similar to a ListBox but with much more versatile options. It provides a number of different ways items can be viewed and items can also have subitems that contain information that is related to the parent item.\n<\/p>\n<h4>\nAlternatives to List&nbsp;View<br \/>\n<\/h4>\n<p>\nIf you want to use images in columns other than column 1 or buttons then a datagrid view is usually a better choice than the List View.\n<\/p>\n<h4>\nUsing a ListView like ListBox but with options for each list item<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\t\/\/----- SETUP LIST VIEW BOX -----\r\n\tSystem::Windows::Forms::Padding ^Lv1Margin = gcnew System::Windows::Forms::Padding();\r\n\tLv1Margin = listView1-&gt;Margin;\r\n\tlistView1-&gt;Columns-&gt;Add(&quot;&quot;, (listView1-&gt;Width - Lv1Margin-&gt;Left - Lv1Margin-&gt;Right - SystemInformation::VerticalScrollBarWidth), HorizontalAlignment::Left);\t\/\/Add a single column\r\n\tlistView1-&gt;HeaderStyle = System::Windows::Forms::ColumnHeaderStyle::None;\t\t\t\t\t\t\t\t\t\t\/\/Don&#39;t display colum headings\r\n\tthis-&gt;listView1-&gt;View = System::Windows::Forms::View::Details;\t\t\t\t\t\t\t\t\t\t\t\t\t\/\/Details view\r\n\tlistView1-&gt;FullRowSelect = true;\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\/\/Highlight the entire row\r\n\r\n\tlistView1-&gt;SmallImageList = ilListView1;\t\t\t\t\/\/Optional if you want an icon (create image list seperately)\r\n<\/code><\/pre>\n<h4>\nImportant Properties<br \/>\n<\/h4>\n<p>\nView &#8211; select the typoe of list view (icons, list, tiles, etc- you need to select &#39;Details&#39; to&nbsp;see multiple columns, grid lines etc)\n<\/p>\n<p>\nMultiSelect&nbsp;&#8211;&nbsp;true or false\n<\/p>\n<p>\nHideSelection&nbsp;&#8211; true if&nbsp;you always want full row to be highlighted when selected\n<\/p>\n<p>\nFullRowSelect\n<\/p>\n<p>\nState Image List &#8211; Set to none if you&#39;re not using images to remove the space left for them on the left of the 1st column\n<\/p>\n<p>\nGrid Lines\n<\/p>\n<h4>\nAdding Rows<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tListViewItem ^ListViewItem1;\r\n\r\n\tfor (Count = 0; Count &lt; MyResultsToDisplay-&gt;Length; Count++)\r\n\t{\r\n\t\tListViewItem1 = gcnew ListViewItem();\r\n\t\tListViewItem1-&gt;Text = &quot;a&quot;;\r\n\r\n\t\t\/\/Set other parameters as required...\r\n\r\n\t\tlstResults-&gt;Items-&gt;Add(ListViewItem1);\r\n\t}\r\n<\/code><\/pre>\n<h4>\nSet Row Colour<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tListViewItem1-&gt;BackColor = System::Drawing::Color::Green;\r\n\tListViewItem1-&gt;ForeColor = System::Drawing::Color::Red;\r\n<\/code><\/pre>\n<h4>\nClear selection<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tfor (Count = 0; Count &lt; listView1-&gt;Items-&gt;Count; Count--)\r\n\t\tlistView1-&gt;Items[Count]-&gt;Selected = false;\r\n<\/code><\/pre>\n<h4>\nSetting Text Of Mutliple Columns<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tListViewItem1-&gt;Text = &quot;a&quot;;\r\n\tListViewItem1-&gt;SubItems-&gt;Add(&quot;b&quot;);\r\n\tListViewItem1-&gt;SubItems-&gt;Add(&quot;c&quot;);\r\n<\/code><\/pre>\n<h4>\nIs Item Selected?<br \/>\n<\/h4>\n<pre>\r\n<code>\tlistView1-&gt;Items[LastTagEventPassedArrayIndex]-&gt;Selected = true;<\/code><\/pre>\n<h4>\nWhich Items Is Selected?<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tfor (SelectedIndex = 0; SelectedIndex &lt; lstResults-&gt;Items-&gt;Count; SelectedIndex++)\r\n\t{\r\n\t\tif (listView1-&gt;Items[SelectedIndex]-&gt;Selected)\r\n\t\t\tbreak;\r\n\t}\r\n<\/code><\/pre>\n<h4>\nImage List Icons<br \/>\n<\/h4>\n<p>\nCreate images as 96dpi .png for optimum quality. Creating at 72dpi will cause images to degrade when displayed.\n<\/p>\n<h4>\nSelect An Item<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tlvMyListView-&gt;Items[3]-&gt;Selected = true;\r\n\tlvMyListView-&gt;Select();\r\n\tlvMyListView-&gt;EnsureVisible(3);\t\t\/\/Scroll to selected item\r\n<\/code><\/pre>\n<h4>\nScroll To Item<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tlvMyListView-&gt;EnsureVisible(3);\r\n<\/code><\/pre>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The ListView control is similar to a ListBox but with much more versatile options. It provides a number of different ways items can be viewed and items can also have subitems that contain information that is related to the parent item. Alternatives to List&nbsp;View If you want to use images in columns other than column [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[],"class_list":["post-339","post","type-post","status-publish","format-standard","hentry","category-list-view"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/339","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=339"}],"version-history":[{"count":20,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/339\/revisions"}],"predecessor-version":[{"id":1474,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/339\/revisions\/1474"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}