// Method for handling going to the very first element of the list (the one with index 0) and havig is displayed: private void initial_list_element_Click(object sender, EventArgs e) { if (catalogCarEntry.Count() != 0) // Making sure the list is not empty. { // First set the counter to 0: counter = 0; // Put the initial element of the catalogCarEntry list // into the 'currentRecord' record of type RecordSerializable: currentRecord = catalogCarEntry[counter]; // store RecordSerializable values in temporary string array var values = new string[] { currentRecord.Account.ToString(), currentRecord.FirstName.ToString(), currentRecord.LastName.ToString(), currentRecord.Balance.ToString() }; // copy string-array values to TextBox values SetTextBoxValues(values); } else { MessageBox.Show("List Is Empty!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); // Display empty text boxes: ClearTextBoxes(); } }
// parameterless constructor public Form1() { InitializeComponent(); // Creation of a List with catalog entries which is to help // in development and testing out the different functinalities // of the program: // Creation of 4 catalog entries. Please note that each entry has 4 fields, // namely 'number in catalog', 'car brand', 'model' and 'rental cost per day'. // var dummyRecord = new RecordSerializable(0, "", "", 0.00M); var record1 = new RecordSerializable(1, "Ford", "Fiesta", 68.37M); var record2 = new RecordSerializable(2, "Chevrolet", "Corvette", 232.54M); var record3 = new RecordSerializable(3, "Dodge", "Ranger", 170.33M); var record4 = new RecordSerializable(4, "Fiat", "Uno", 250.45M); // Write 4 record entries into the list 'catalogCarEntry': catalogCarEntry.Add(record1); catalogCarEntry.Add(record2); catalogCarEntry.Add(record3); catalogCarEntry.Add(record4); // Creation and initialization of a variable to measure the length // of the list. This will serve an important purpose in checking // errors and catching exception, like for example when the button // 'Step Up' has been pressed too often: int catalogLength = catalogCarEntry.Capacity; }
} // end method openButton_Click // invoked when user clicks Next button private void nextButton_Click(object sender, EventArgs e) { // deserialize RecordSerializable and store data in TextBoxes try { // get next RecordSerializable available in file RecordSerializable record = ( RecordSerializable )reader.Deserialize(input); // store RecordSerializable values in temporary string array string[] values = new string[] { record.Account.ToString(), record.FirstName.ToString(), record.LastName.ToString(), record.Balance.ToString() }; // copy string array values to TextBox values SetTextBoxValues(values); } // end try // handle exception when there are no RecordSerializables in file catch (SerializationException) { input.Close(); // close FileStream openButton.Enabled = true; // enable Open File button nextButton.Enabled = false; // disable Next Record button ClearTextBoxes(); // notify user if no RecordSerializables in file MessageBox.Show("No more records in file", string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information); } // end catch } // end method nextButton_Click
} // end method saveButton_Click // handler for enterButton Click private void enterButton_Click(object sender, EventArgs e) { // store TextBox values string array string[] values = GetTextBoxValues(); // RecordSerializable containing TextBox values to serialize RecordSerializable record = new RecordSerializable(); // determine whether TextBox account field is empty if (values[( int )TextBoxIndices.ACCOUNT] != string.Empty) { // store TextBox values in RecordSerializable and serialize it try { // get account number value from TextBox int accountNumber = Int32.Parse( values[( int )TextBoxIndices.ACCOUNT]); // determine whether accountNumber is valid if (accountNumber > 0) { // store TextBox fields in RecordSerializable record.Account = accountNumber; record.FirstName = values[( int ) TextBoxIndices.FIRST]; record.LastName = values[( int ) TextBoxIndices.LAST]; record.Balance = Decimal.Parse(values[ ( int )TextBoxIndices.BALANCE]); // write Record to FileStream ( serialize object ) formatter.Serialize(output, record); } // end if else { // notify user if invalid account number MessageBox.Show("Invalid Account Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } // end else } // end try // notify user if error occurs in serialization catch (SerializationException) { MessageBox.Show("Error Writing to File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } // end catch // notify user if error occurs regarding parameter format catch (FormatException) { MessageBox.Show("Invalid Format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } // end catch } // end if ClearTextBoxes(); // clear TextBox values } // end method enterButton_Click
// Method for handling deletion of element: private void delete_current_element_Click(object sender, EventArgs e) { if (catalogCarEntry.Count() >= 2) { catalogCarEntry.RemoveAt(counter); // Now find out whether the element moved was the highest on in the list: if ((counter + 1) > catalogCarEntry.Count()) // I added 1 to counter due to its 0 initial value. { counter = counter - 1; } // Otherwise the counter remains the same: currentRecord = catalogCarEntry[counter]; var nowValues = new string[] { currentRecord.Account.ToString(), currentRecord.FirstName.ToString(), currentRecord.LastName.ToString(), currentRecord.Balance.ToString() }; // Display the contents of the element indicated by the counter now: SetTextBoxValues(nowValues); } else if (catalogCarEntry.Count() == 1) // If there is only one element in the element. { catalogCarEntry.RemoveAt(counter); // Now the list should be empty and hence only contain // the element catalogCarEntry[0]. MessageBox.Show("You Just Removed the Last Element! Now the List is Empty!", "Announcement", MessageBoxButtons.OK, MessageBoxIcon.Error); counter = 0; // Counter put to zero by default. Actually there isn't any element in an empty list. // In any case a good starting point for subsequent stages in the program. // Display empty text boxes: ClearTextBoxes(); } else { MessageBox.Show("List Was Empty to Begin with!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); counter = 0; // counter put to zero by default. Actually there is no elemet in an empty list. // Display empty text boxes: ClearTextBoxes(); } }
// handler for enterButton Click private void enterButton_Click(object sender, EventArgs e) { // store TextBox values string array string[] values = GetTextBoxValues(); // determine whether TextBox account field is empty if (!string.IsNullOrEmpty(values[(int)TextBoxIndices.Account])) { // store TextBox values in RecordSerializable and serialize it try { // get account-number value from TextBox int accountNumber = int.Parse( values[(int)TextBoxIndices.Account]); // determine whether accountNumber is valid if (accountNumber > 0) { // RecordSerializable to serialize var record = new RecordSerializable(accountNumber, values[(int)TextBoxIndices.First], values[(int)TextBoxIndices.Last], decimal.Parse(values[(int)TextBoxIndices.Balance])); // write Record to FileStream (serialize object) formatter.Serialize(output, record); } else { // notify user if invalid account number MessageBox.Show("Invalid Account Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (SerializationException) { MessageBox.Show("Error Writing to File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (FormatException) { MessageBox.Show("Invalid Format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } ClearTextBoxes(); // clear TextBox values }
// Method for handling loading records from an external file (presumably .SER file) into the list named 'catalogCarEntry' // which is for elements of type 'RecordSerializable': private void load_from_binary_file_Click(object sender, EventArgs e) { // Create and show dialog box enabling user to open file: DialogResult result; // result of OpenFileDialog. string inputfileName; using (OpenFileDialog inputfileChooser = new OpenFileDialog()) { result = inputfileChooser.ShowDialog(); inputfileName = inputfileChooser.FileName; // Get specified name. } // Ensure that user clicked "OK": if (result == DialogResult.OK) { ClearTextBoxes(); // Show error if user specified invalid file: if (string.IsNullOrEmpty(inputfileName)) { MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { // Create FileStream to obtain read access to file: inputstream = new FileStream( inputfileName, FileMode.Open, FileAccess.Read); // openButton.Enabled = false; // disaable Open File button. (Might be irrelevant here.) // nextButton.Enable = true; // enable Next Record button. (Might be irrelevant here.) // In the following code stucture (which will be within this 'else' module, we will put the // contents of the 'stream' into the 'List' nameed 'catalogCarEntry'. Of course we have // incorporated a try and catch system, in case that 'stream' contains something that 'List' // doesn't like. // Deserialize: try { // List<RecordSerializable> catalogCarEntry = null; // Experimental command. // catalogCarEntry = (List<RecordSerializable>)serializer.Deserialize(inputstream); catalogCarEntry = (List <RecordSerializable>)serializer.Deserialize(inputstream); // Show the 0th elemet of the catalogCarEntry list, so that the boxes won't // be empty after the loading procedure: if (catalogCarEntry.Count() != 0) // Making sure the list is not empty. { // First set the counter to 0: counter = 0; // Put the initial element of the catalogCarEntry list // into the 'currentRecord' record of type RecordSerializable: currentRecord = catalogCarEntry[counter]; // store RecordSerializable values in temporary string array var values = new string[] { currentRecord.Account.ToString(), currentRecord.FirstName.ToString(), currentRecord.LastName.ToString(), currentRecord.Balance.ToString() }; // copy string-array values to TextBox values SetTextBoxValues(values); } else { MessageBox.Show("List Is Empty!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); // Display empty text boxes: ClearTextBoxes(); } } catch (SerializationException) { inputstream?.Close(); // close FileStream // openButon.Enabled = true; // Irrelevant here presumably. // nextButton.Enabled = false; // Disable Next Record button. ClearTextBoxes(); // Notify user if no RecordSerializables in file MessageBox.Show("No more records in file", string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information); } } } }
// Method for handling modification of element in question: private void modify_element_Click(object sender, EventArgs e) { /* Here it as about trying what the getBox Method has yielded, * and catching the string array, named values presumably, for * for format errors. * As for those that made it into the try block, check if the * account value is an integer larger than 0. * */ // Now lets first get the values that are (or are not) in the boxes: string[] values = GetTextBoxValues(); // Now determine if the account field (the integer number entry in the catalog) is empty: if (!string.IsNullOrEmpty(values[(int)TextBoxIndices.Account])) { // store TextBox values in the element of type RecordSerializable in the catalogCarEntry list: try { int accountNumber = int.Parse(values[(int)TextBoxIndices.Account]); // Determine whether accountNumber is valid: if (accountNumber > 0) // Maybe later add a check for correctness of the format of the 'balance field' too. { // Record containing TextBox values to output var tempStorageRecord = new RecordSerializable(accountNumber, values[(int)TextBoxIndices.First], values[(int)TextBoxIndices.Last], decimal.Parse(values[(int)TextBoxIndices.Balance])); // Put that record as an element in its proper place within // the catalogCarEntry list: if (justModifyCurrentValue) // No maniplation of indexes or List elements necessary { if (catalogCarEntry.Count() >= 1) // Making sure the list is not empty. { catalogCarEntry[counter] = tempStorageRecord; // Put a copy of that newly modified element and put it into a temporary record of type // RecordSerializable: currentRecord = catalogCarEntry[counter]; var currentvalues = new string[] { currentRecord.Account.ToString(), currentRecord.FirstName.ToString(), currentRecord.LastName.ToString(), currentRecord.Balance.ToString() }; // Display the contents of the modified element in the text boxes: SetTextBoxValues(currentvalues); } else { MessageBox.Show("List Is Empty! No Element to Modify", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); counter = 0; // counter put to zero by default. Actually there is no elemet in an empty list. // Display empty text boxes: ClearTextBoxes(); } } if (insertValueInCurrentLocation) // Here some maniplation of indexes or List-elements necessary { if (catalogCarEntry.Count() >= 1) // Making sure the list is not empty. { // Insert an element to the list 'catalogCarEntry' at the location specified // by the current value of the counter: catalogCarEntry.Insert(counter, tempStorageRecord); // Put a copy of that newly inserted element and put it into a temporary record of type // RecordSerializable: currentRecord = catalogCarEntry[counter]; var insertedcurrentvalues = new string[] { currentRecord.Account.ToString(), currentRecord.FirstName.ToString(), currentRecord.LastName.ToString(), currentRecord.Balance.ToString() }; // Display the contents of the inserted element in the text boxes: SetTextBoxValues(insertedcurrentvalues); } else { // catalogCarEntry[0] = tempStorageRecord; // If the list is empty, it is transformed into one with one element. catalogCarEntry.Add(tempStorageRecord); // Put a copy of that newly inserted element and put it into a temporary record of type // RecordSerializable: currentRecord = catalogCarEntry[0]; var insertedcurrentvalues = new string[] { currentRecord.Account.ToString(), currentRecord.FirstName.ToString(), currentRecord.LastName.ToString(), currentRecord.Balance.ToString() }; // Display the contents of the inserted element in the text boxes: SetTextBoxValues(insertedcurrentvalues); } } if (appendValueToEndOfList) // Here an element is added to the end of the list: { if (catalogCarEntry.Count() >= 1) // Making sure the list is not empty. { // Append an element to the list 'catalogCarEntry' at the very end: // by the current value of the counter: catalogCarEntry.Add(tempStorageRecord); // This appends the tempStorageRecord to the end of the list. // Now display the newly appended element that is at the very end of the modified list: // First get the index value of the last element which is the number of elements minus one. // (Minus one because the indexes start at zero.) counter = catalogCarEntry.Count() - 1; // Put a copy of that newly appended element and put it into a temporary record of type // RecordSerializable: currentRecord = catalogCarEntry[counter]; // store RecordSerializable values in temporary string array var appendedvalues = new string[] { currentRecord.Account.ToString(), currentRecord.FirstName.ToString(), currentRecord.LastName.ToString(), currentRecord.Balance.ToString() }; // Display the contents of the appended element in the text boxes: SetTextBoxValues(appendedvalues); } else { // catalogCarEntry.Add(tempStorageRecord); // If the list is empty, it is transformed into one with one element // consisting of the newly added record. // I think you get a one element list if you do this to an empty list. // I think it has been assured here that here catalogCarEntry can't be anything but an empty List. // Perhaps some extra test here would be advisable: catalogCarEntry.Add(tempStorageRecord); // Put a copy of that newly appended element and put it into a temporary record of type // RecordSerializable: currentRecord = catalogCarEntry[0]; var appendedvalues = new string[] { currentRecord.Account.ToString(), currentRecord.FirstName.ToString(), currentRecord.LastName.ToString(), currentRecord.Balance.ToString() }; // Display the contents of the appended element in the text boxes: SetTextBoxValues(appendedvalues); } } } else { // Notifying the user if the account number was invalid MessageBox.Show("Invalid Account Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } // It can be mentioned here that I think catching serialization is not relevant here. catch (FormatException) { MessageBox.Show("Invalid Format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } // Return to the default settings of in terms of which buttons are enabled and not enabled: step_down.Enabled = true; step_up.Enabled = true; add_element_current_location.Enabled = true; add_element_end_of_list.Enabled = true; initial_list_element.Enabled = true; load_from_binary_file.Enabled = true; save_to_binary_file.Enabled = true; delete_current_element.Enabled = true; modify_element.Enabled = true; // Back to the default settings as regards for which of the 3 types of modifications to implement: justModifyCurrentValue = true; insertValueInCurrentLocation = false; appendValueToEndOfList = false; }
// Method for handing step up click: private void step_up_Click(object sender, EventArgs e) { if (catalogCarEntry.Count() != 0) // Making sure the list is not empty. { // If list is not empty go up the list when possible: if ((counter + 1) == catalogCarEntry.Count()) // Check if counter is currently on the last (highest) element { // of the list. Note that one needs to be added, because inexing // starts at 0. MessageBox.Show("Upper Bound of Catalog Reached", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); // The counter is not incremented. It remains in the value that it was in before entering the module. } else { counter = counter + 1; } currentRecord = catalogCarEntry[counter]; // store RecordSerializable values in temporary string array var values = new string[] { currentRecord.Account.ToString(), currentRecord.FirstName.ToString(), currentRecord.LastName.ToString(), currentRecord.Balance.ToString() }; // It can't be overemphasized how important it is to keep track of the current counter value // as well as the overalall length of the list (the number of elements in it) // in the entire program. // determine whether TextBox account field is empty if (!string.IsNullOrEmpty(values[(int)TextBoxIndices.Account])) // this seems also to check perhaps if // list has no element at all, provided // a conversion of an empty list of // RecordSeriaizable elements gets // transformed into a Null during // the transformation to the string // array above. { // store TextBox values in RecordSerializable and set to the // text boxes, provided that everything is OK regarding format // and account number: try { // get account-number value from TextBox int accountNumber = int.Parse( values[(int)TextBoxIndices.Account]); // determine whether accountNumber is valid if (accountNumber > 0) { // copy string-array values to TextBox values SetTextBoxValues(values); } else { // notify user if invalid account number MessageBox.Show("Invalid Account Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (FormatException) { MessageBox.Show("Invalid Format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("List is Empty!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } // ClearTextBoxes(); // clear TextBox values. I don't think it is needed here. } }
// Method for handling step down click: private void step_down_Click(object sender, EventArgs e) { if (catalogCarEntry.Count() != 0) // Making sure the list is not empty. { // lower counter by 1, when possible: if (counter <= 0) { counter = 0; MessageBox.Show("Lower Bound of Catalog Reached", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { counter = counter - 1; } currentRecord = catalogCarEntry[counter]; // store RecordSerializable values in temporary string array var values = new string[] { currentRecord.Account.ToString(), currentRecord.FirstName.ToString(), currentRecord.LastName.ToString(), currentRecord.Balance.ToString() }; // It can't be overemphasized how important it is to keep track of the current counter value // as well as the overalall length of the list (the number of elements in it) // in the entire program. // determine whether TextBox account field is empty if (!string.IsNullOrEmpty(values[(int)TextBoxIndices.Account])) // this seems also to check perhaps // if list has not element, provided // a conversion of an empty list of // RecordSeriaizable elements gets // transformed into a Null during // the transformation to the string // array above. { // Checking first format of account number is OK. If not format exception. // If format is OK, then check if number is greater that 0. Eventually // display contents of the RecordSerializable as strings in the // corresponding text boxes: try { // get account-number value from TextBox int accountNumber = int.Parse( values[(int)TextBoxIndices.Account]); // determine whether accountNumber is valid if (accountNumber > 0) { // copy string-array values to TextBox values SetTextBoxValues(values); } else { // notify user if invalid account number MessageBox.Show("Invalid Account Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (FormatException) { MessageBox.Show("Invalid Format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("List is Empty!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
// Method for handling loading of the form: private void Form1_Load(object sender, EventArgs e) { // See to it that by default the first element of the list (element with index 0) gets displayed // when the file is loaded. // Should the list be empty there will be a corresponding error handling procedure. // Note that the counter has been declared 0 at the very beginning of the program, so I won't be doing it // again here. if (catalogCarEntry.Count() != 0) // Making sure the list is not empty. { currentRecord = catalogCarEntry[counter]; // store RecordSerializable values in temporary string array var values = new string[] { currentRecord.Account.ToString(), currentRecord.FirstName.ToString(), currentRecord.LastName.ToString(), currentRecord.Balance.ToString() }; // It can't be overemphasized how important it is to keep track of the current counter value // as well as the overalall length of the list (the number of elements in it) // in the entire program. // determine whether TextBox account field is empty if (!string.IsNullOrEmpty(values[(int)TextBoxIndices.Account])) // this seems also to check perhaps // if list has not element, provided // a conversion of an empty list of // RecordSeriaizable elements gets // transformed into a Null during // the transformation to the string // array above. { // store TextBox values in RecordSerializable and setting to text // box values, provided that everything is OK as regards for // format and account number: try { // get account-number value from TextBox int accountNumber = int.Parse( values[(int)TextBoxIndices.Account]); // determine whether accountNumber is valid if (accountNumber > 0) { // copy string-array values to TextBox values SetTextBoxValues(values); } else { // notify user if invalid account number MessageBox.Show("Invalid Account Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (FormatException) { MessageBox.Show("Invalid Format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("List is Empty!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }