/*---------------------------------------------------------------------- USING COUNTED LOOPS Ron Kessler 5/4/2016 How to fill LB with numbers in a for loop It also shows you how to find out which item they chose ----------------------------------------------------------------------- Updated 5/4/2016 for C++ 2015 */ #pragma once namespace CountedLoops { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: Add the constructor code here // } //************Start of my code ****************** private: System::Void btnAdd_Click(System::Object^ sender, System::EventArgs^ e) { //---enter numbers in ascending order int max = Convert::ToInt32(txtTotalItems->Text); if (max > 0) { lstItems->Items->Clear(); for (int x = 1; x <= max; x++) lstItems->Items->Add(x); } } private: System::Void btnBackWards_Click(System::Object^ sender, System::EventArgs^ e) { //---enter numbers in descending order int max = Convert::ToInt32(txtTotalItems->Text); if (max > 0) { lstItems->Items->Clear(); for (int x = max; x >0; x--) lstItems->Items->Add(x); } } private: System::Void lstItems_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) { //---SelectedIndex gives you the integer value of the selected item Label1->Text = "You selected item with index # " + lstItems->SelectedIndex; //---selectedItem returns the text of the item chosen Label2->Text = "You chose " + lstItems->SelectedItem; } }; }