/*__________________________________________ C S 1 3 1 D A T A S T R U C T U R E S W O R K I N G W I T H E N U M E R A T I O N S Ron Kessler Created 6/2/2015 __________________________________________ This project shows how to use enumerations. Enums are a collection of constants. When you need to keep tract of related data but want to use friendly names, this is your tool. CAUTION: Do not sort the list boxes when you are loading them with enums because we are loading them based on the enum values you define. The listbox will sort them alphabetically and this will mess everything up! */ #pragma once public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); this->CenterToScreen(); } //************START OF MY CODE************ #pragma region Declarations static String^ MSGBOX_TITLE = "Kessler's Store"; //---create enums public: enum class WeekDays { Monday = 1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; public: enum class CreditCard { //--- I added some fake codes. Even if you change them the code works. VISA = 1, MasterCard = 3, AMEX = 22, Discover = 11 }; public: enum class myColors { Blue, Red, Green }; #pragma endregion #pragma region Form Load //---load payment options & color options private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { for each(String^ whichCard in Enum::GetNames(CreditCard::typeid)) { lbCreditCard->Items->Add(whichCard); } for each(String^ whichColor in Enum::GetNames(myColors::typeid)) { lbColors->Items->Add(whichColor); } } #pragma endregion #pragma region Part 1 //**************---Part 1:**************** //Create an enum for the days of the week. I tell Monday to = 1 and then Tues = 2, Wed=3 automatically. // Hover your mouse over one of the days to see the assigned value. Often, enums use integers like this // but you can use other data types also as you will see. //---lets fill the listbox with the days of the week private: System::Void btnFillDays_Click(System::Object^ sender, System::EventArgs^ e) { for each(String^ day in Enum::GetNames(WeekDays::typeid)) { lbDays->Items->Add(day); } } private: System::Void lbDays_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) { //---now display the index number of the selected item Array^ enumValues = Enum::GetValues(WeekDays::typeid); //get all the values Object^ enumIndex = enumValues->GetValue(lbDays->SelectedIndex); //pick the selected one //---now show the selected enums value lblDaysIndex->Text ="Enum value = " + Enum::Format(WeekDays::typeid, enumIndex, "D"); //D = value in decimal form } #pragma endregion #pragma region Part 2 private: System::Void btnTerms_Click(System::Object^ sender, System::EventArgs^ e) { if(lbCreditCard->SelectedIndex != -1) { //---now let's switch using the enum value but use the friendly name in the case clause. //This way, no matter what the enum value is, we can switch on it! Array^ allValues = Enum::GetValues(CreditCard::typeid); //get all the values Object^ selectedCardValue = allValues->GetValue(lbCreditCard->SelectedIndex); //pick the selected one // be sure to cast selectedCardValue to the enum type. switch ((CreditCard)selectedCardValue) //swich can only use an int, char, or enum type { case CreditCard::AMEX: NotifyUser("American Express",1); break; case CreditCard::Discover: NotifyUser("Discover",1); break; case CreditCard::MasterCard: NotifyUser("Master Card",1); break; case CreditCard::VISA: NotifyUser("VISA",1); break; } } else { NotifyUser("", 0); } } #pragma endregion #pragma region Part 3 Select Colors private: System::Void btnColor_Click(System::Object^ sender, System::EventArgs^ e) { if(lbColors->SelectedIndex != -1) { Array^ allValues = Enum::GetValues(myColors::typeid); //get all the values Object^ selectedColorValue = allValues->GetValue(lbColors->SelectedIndex); //pick the selected one switch ((myColors) selectedColorValue) { case myColors::Blue: this->BackColor = Color::Blue; break; case myColors::Red: this->BackColor = Color::Red; break; case myColors::Green: this->BackColor = Color::Green; break; } } else { NotifyUser("",0); } } #pragma endregion #pragma region Helper Methods private: Void NotifyUser(String^ msg, int picked) { if(picked == 0) msg = "Please make a selection."; else msg = "You selected " + msg + " for payment."; MessageBox::Show(msg, MSGBOX_TITLE, MessageBoxButtons::OK, MessageBoxIcon::Information); } private: System::Void exitToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) { this->Close(); } #pragma endregion }; }