/*_________________________________________________________________________________ S T R E A M R E A D E R / W R I T E R D E M O Traditional Method Originally Created 4/22/2013 for C# Ron Kessler C++ 2014 Version _________________________________________________________________________________ Created 1/24/2014 Last updated 1/24/2014 FEATURES: 1. Shows how to read/write to a sequential text file 2. Lets user create file when program first loads 3. Items can be added/removed from list box 4. Items in list box can be saved to disk 5. Shows how to use Foreach loop with list box items 6. Uses structured error handling * 7. I create Stream objects without the new "using" statement */ #pragma once namespace My1UsingStreamsDemo { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; //********************************* using namespace System::IO; //be sure to add this //********************************* //**********START OF MY CODE************* static String^ diskFile = "inventory.txt"; static String^ msgTitle = "Ron's Train Store"; StreamReader^ sr; //define streamreader here without instaniating StreamWriter^ sw; // streamwriter #pragma region "Load Event, Reload Button, and Reload Method" private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { DoReload(); } private: System::Void btnReload_Click(System::Object^ sender, System::EventArgs^ e) { DoReload(); } private: System::Void DoReload() { //---if file does not exist lets create it for them if they want to. if (File::Exists(diskFile) == false) { if (MessageBox::Show("Cannot locate inventory file. Shall I create it?", msgTitle, MessageBoxButtons::YesNo, MessageBoxIcon::Question) ==System::Windows::Forms::DialogResult::Yes) File::Create(diskFile); else txtNewItem->Focus(); //or you could exit here: this.close(); } else { try { lstItems->Items->Clear(); //no duplicates sr = gcnew StreamReader(diskFile); { while (sr->Peek() != -1) { String^ dataFromFile = sr->ReadLine(); lstItems->Items->Add(dataFromFile); } } } catch (Exception^ e) { MessageBox::Show("An error occurred while reading...." + e->Message, msgTitle, MessageBoxButtons::OK, MessageBoxIcon::Error); this->Close(); } finally { sr->Close(); //make sure to always close the reader } } } #pragma endregion #pragma region "Add new Item & Delete Item" private: System::Void btnAdd_Click(System::Object^ sender, System::EventArgs^ e) { String^ myNewItem = txtNewItem->Text->Trim(); lstItems->Items->Add(myNewItem); } private: System::Void btnDelete_Click(System::Object^ sender, System::EventArgs^ e) { if (MessageBox::Show("Are you sure you want to delete " + lstItems->SelectedItem + "?", msgTitle, MessageBoxButtons::YesNo, MessageBoxIcon::Question) == System::Windows::Forms::DialogResult::Yes) lstItems->Items->Remove(lstItems->SelectedItem); else txtNewItem->Focus(); } #pragma endregion #pragma region "Save Data to Disk" private: System::Void btnSave_Click(System::Object^ sender, System::EventArgs^ e) { try { sw = gcnew StreamWriter(diskFile, false); //false means do not append data! for each (String^ myItem in lstItems->Items) { sw->WriteLine(myItem); } MessageBox::Show("Your data has been updated.", msgTitle, MessageBoxButtons::OK, MessageBoxIcon::Information); } catch (Exception^ ex) { MessageBox::Show("I could not save your changes." + ex->Message, msgTitle, MessageBoxButtons::OK, MessageBoxIcon::Error); } finally { sw->Close(); //always do this } } #pragma endregion #pragma region "Clear & Exit" private: System::Void btnClear_Click(System::Object^ sender, System::EventArgs^ e) { lstItems->Items->Clear(); txtNewItem->Clear(); } private: System::Void btnExit_Click(System::Object^ sender, System::EventArgs^ e) { this->Close(); } #pragma endregion }; }