/*_________________________________________________________________________________ U S I N G A L I S T & L I S T V I E W C O N T R O L Ron Kessler Created 10/18/2014 VS 2012 C++/CLI _________________________________________________________________________________ Updated 10/22/2014 Features: 1. How to use the List to hold a collection of items 2. How to use a ListView Control to display items that are in memory without using a listbox control. 3. How to access members of another class from the main form. */ #pragma once #include "Employee.h"; #include "General.h"; public ref class Form1 : public System::Windows::Forms::Form { //---create new instance of employee class static Employee^ myEmployee= gcnew Employee; public: Form1(void) { InitializeComponent(); this->CenterToScreen(); private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { bool loadedNames = false; loadedNames = myEmployee->LoadNames(); if(loadedNames = false) { General::ShowMSG( "I could not load the list of employees.", General::generalMsgTitle); this->Close(); } else DisplayAll(); } #pragma region Add & Delete Buttons private: System::Void btnAdd_Click(System::Object^ sender, System::EventArgs^ e) { if(txtNew->Text->Trim() =="") { General::ShowMSG("Please enter a name.", General::addMsgTitle); txtNew->Clear(); txtNew->Focus(); } else { myEmployee->NewName = txtNew->Text->Trim(); if(myEmployee->AddNewPerson() ==true) { DisplayAll(); txtNew->Clear(); txtNew->Focus(); } } } private: System::Void btnDelete_Click(System::Object^ sender, System::EventArgs^ e) { if(txtDelete->Text->Trim() =="") { General::ShowMSG("Please enter a name.", General::deleteMsgTitle); txtNew->Clear(); txtNew->Focus(); } else { myEmployee->DeleteName = txtDelete->Text->Trim(); if(myEmployee->DeletePerson()==true) { DisplayAll(); txtDelete->Clear(); txtDelete->Focus(); } } } #pragma endregion #pragma region Display all Employees public: void DisplayAll() { //now use that variable to access the controls on form1 ListView1->Clear(); myEmployee->employeeNames->Sort(); //---here I create a variable called listItem as string within the loop itself! for each(String^ listItem in myEmployee->employeeNames) { ListView1->Items->Add(listItem); ListView1->Sort(); } } #pragma endregion