/*_________________________________________________________________________ U S I N G S W I T C H S T A T E M E N T S Created 1/23/2014 for C++ 2012 Ron Kessler ___________________________________________________________________________ Updated 1/24/2014 1/24/2014 Added a combo box to display the states instead of using a textbox This program demonstrates how to use the switch structure. They choose a State & we compute the sales tax rate. C++ will not let you use strings in switch. It will let you evaluate a char, int and other value types. But since I want to evaluate states (CA, AZ, etc.) I put them into a combo box and switch on the selected index. Remember, combo/list boxes start indexing from zero. Also shows how to use the Accept and Cancel buttons */ #pragma once namespace My3UsingSwitch { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// /// Summary for Form1 /// public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: Add the constructor code here // } protected: /// /// Clean up any resources being used. /// ~Form1() { if (components) { delete components; } } internal: System::Windows::Forms::Button^ btnQuit; protected: internal: System::Windows::Forms::Button^ btnOK; internal: System::Windows::Forms::Label^ Label5; internal: System::Windows::Forms::Label^ Label3; internal: System::Windows::Forms::TextBox^ txtCity; internal: System::Windows::Forms::Label^ Label2; internal: System::Windows::Forms::TextBox^ txtName; internal: System::Windows::Forms::Label^ Label1; private: System::Windows::Forms::ComboBox^ cboStates; internal: private: /// /// Required designer variable. /// System::ComponentModel::Container ^components; //***********START OF MY CODE************ static double salesTaxRate = 0; //class level variable private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { //---fill the combo box cboStates->Items->Add("CA"); cboStates->Items->Add("AZ"); cboStates->Items->Add("NV"); cboStates->Items->Add("OR"); cboStates->Items->Add("WA"); cboStates->SelectedIndex =0; // make sure the first one is selected } private: System::Void btnOK_Click(System::Object^ sender, System::EventArgs^ e) { //---now decide the sales tax rate based upon the state they chose MessageBox::Show("The sales tax for " + cboStates->SelectedItem->ToString() + " is " + salesTaxRate + ".", "Using Switch" , MessageBoxButtons::OK, MessageBoxIcon::Information); } private: System::Void cboStates_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) { /* ---now decide the sales tax rate based upon the state they chose. C++ will not let you evaluate a string using switch like you can in VB & C#. That is why it is easier to use a combo box and switch on the index number. */ switch (cboStates->SelectedIndex) { case 0: //CA salesTaxRate = .08; break; case 1: //AZ salesTaxRate = .05; break; case 2: //NV salesTaxRate = .01; break; case 3: //OR and WA are the same. If they choose OR, the switch "falls through" to case 4. Both OR and WA will have the same sales tax case 4: //WA salesTaxRate = .06; break; } } private: System::Void btnQuit_Click(System::Object^ sender, System::EventArgs^ e) { this->Close(); } }; }