/*_______________________________________________ S I M P L E S T A C K D E M O Ron Kessler Created 7/6/2015 This project demonstrates how a stack structure works. You add names to the stack and then when you pop the items off the stack they will be added to the listbox. The stack is based on LIFO (Last in, first out). _________________________________________________ */ #pragma once namespace StackDemo { using namespace System; using namespace System::ComponentModel; using namespace System::Collections::Generic; //add generic using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /**************START OF MY CODE**************** //btnAdd is set as the AcceptButton on the form #pragma region Declarations & Form Activated static Stack^ customersStack = gcnew Stack(); //create our stack structure static String^ MSGTITLE = "Working with Stack Structures"; //msgbox caption private: System::Void Form1_Activated(System::Object^ sender, System::EventArgs^ e) { txtNewItem->Focus(); //set cursor to start with } #pragma endregion #pragma region Add to Stack & Listbox private: System::Void btnAdd_Click(System::Object^ sender, System::EventArgs^ e) { //---add items to the stack and the first listbox so we can see the order entered if(txtNewItem->Text !="" ) { String^ custName = txtNewItem->Text->Trim(); lstItems->Items->Add(custName); //---push custName onto the stack. customersStack->Push(custName); //---make it easy to add another name txtNewItem->Clear(); txtNewItem->Focus(); } else NotifyUser("Please enter a customer name."); } #pragma endregion #pragma region POP Items OFF Stack private: System::Void btnPOP_Click(System::Object^ sender, System::EventArgs^ e) { lstStackItems->Items->Clear(); //Start fresh while (customersStack->Count > 0) //make sure there is something on the stack { lstStackItems->Items->Add(customersStack->Pop()); //POP removes the TOP item } } #pragma endregion #pragma region Notify User & Clear Items & Exit private:System::Void NotifyUser(String^ msg) { MessageBox::Show(msg, MSGTITLE, MessageBoxButtons::OK, MessageBoxIcon::Exclamation); } private: System::Void btnClearItems_Click(System::Object^ sender, System::EventArgs^ e) { lstItems->Items->Clear(); txtNewItem->Clear(); txtNewItem->Focus(); } private: System::Void btnClearStackItems_Click(System::Object^ sender, System::EventArgs^ e) { lstStackItems->Items->Clear(); } private: System::Void btnClose_Click(System::Object^ sender, System::EventArgs^ e) { this->Close(); } #pragma endregion