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 View

If 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.

Using a ListView like ListBox but with options for each list item


	//----- SETUP LIST VIEW BOX -----
	System::Windows::Forms::Padding ^Lv1Margin = gcnew System::Windows::Forms::Padding();
	Lv1Margin = listView1->Margin;
	listView1->Columns->Add("", (listView1->Width - Lv1Margin->Left - Lv1Margin->Right - SystemInformation::VerticalScrollBarWidth), HorizontalAlignment::Left);	//Add a single column
	listView1->HeaderStyle = System::Windows::Forms::ColumnHeaderStyle::None;										//Don't display colum headings
	this->listView1->View = System::Windows::Forms::View::Details;													//Details view
	listView1->FullRowSelect = true;																				//Highlight the entire row

	listView1->SmallImageList = ilListView1;				//Optional if you want an icon (create image list seperately)

Important Properties

View – select the typoe of list view (icons, list, tiles, etc- you need to select 'Details' to see multiple columns, grid lines etc)

MultiSelect – true or false

HideSelection – true if you always want full row to be highlighted when selected

FullRowSelect

State Image List – Set to none if you're not using images to remove the space left for them on the left of the 1st column

Grid Lines

Adding Rows


	ListViewItem ^ListViewItem1;

	for (Count = 0; Count < MyResultsToDisplay->Length; Count++)
	{
		ListViewItem1 = gcnew ListViewItem();
		ListViewItem1->Text = "a";

		//Set other parameters as required...

		lstResults->Items->Add(ListViewItem1);
	}

Set Row Colour


	ListViewItem1->BackColor = System::Drawing::Color::Green;
	ListViewItem1->ForeColor = System::Drawing::Color::Red;

Clear selection


	for (Count = 0; Count < listView1->Items->Count; Count--)
		listView1->Items[Count]->Selected = false;

Setting Text Of Mutliple Columns


	ListViewItem1->Text = "a";
	ListViewItem1->SubItems->Add("b");
	ListViewItem1->SubItems->Add("c");

Is Item Selected?

	listView1->Items[LastTagEventPassedArrayIndex]->Selected = true;

Which Items Is Selected?


	for (SelectedIndex = 0; SelectedIndex < lstResults->Items->Count; SelectedIndex++)
	{
		if (listView1->Items[SelectedIndex]->Selected)
			break;
	}

Image List Icons

Create images as 96dpi .png for optimum quality. Creating at 72dpi will cause images to degrade when displayed.

Select An Item


	lvMyListView->Items[3]->Selected = true;
	lvMyListView->Select();
	lvMyListView->EnsureVisible(3);		//Scroll to selected item

Scroll To Item


	lvMyListView->EnsureVisible(3);

 

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *