List Box With More Options

Need columns, colouring, icons etc? Use a List View

Creating a List Box

SelectionMode – Decide how many lines may be selected at a time by the user
MultiColumn – Not what you think! A multicolumn ListBox places items into as many columns as are needed to make vertical scrolling unnecessary.
Sorted – Allows the list to be automatically sorted
Text – Read to get currently selected item, write to cause it to move to matching item

Erasing the contents of a List Box


Erasing Individual Items In A List Box

	while (lstMySelectOptions->Items->Count)
		lstMySelectOptions->Items->RemoveAt(0);	//(RemoveAt works on index number)

Adding items to a List Box

	lstMySelectOptions->BeginUpdate();		//Stop painting of the ListBox as items are added
	lstMySelectOptions->Items->Add("Item A");
	lstMySelectOptions->Items->Add("Item B");

Getting Currently Selected Item

	if (lstMySelectOptions->SelectedIndex >= 0)		//Ensure a list item is selected (-1 if not)
		UserId = lstMySelectOptions->SelectedIndex;

Selecting Items

   lstMySelectOptions->SetSelected(1, true);
   lstMySelectOptions->SetSelected(3, true);
   lstMySelectOptions->SetSelected(5, true);

Colouring Items

Use a ListView instead – gives full options over each item in the list.

Double Click

The double click point will be the current selectedindex

	private: System::Void lstResults_MouseDoubleClick(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e)
		btnViewData_Click(this, gcnew EventArgs());


We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.


Your email address will not be published.