/// <summary> /// Saves the user input to the .txt file when the Save button is clicked /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnSave_Click(object sender, EventArgs e) { // use streamwriter to write StreamWriter outputFile; try { // obtain user input and format for save string inputToSave = $"{TxtBoxCollege.Text}, {TxtBoxCity.Text}"; // open outputFile = File.AppendText(filepath); // use outputFile.WriteLine(inputToSave); // close outputFile.Close(); // clear results and reset cursor focus to first box TxtBoxCollege.Clear(); TxtBoxCity.Clear(); TxtBoxCollege.Focus(); // update LstBox LstBoxColleges.Items.Add(inputToSave); // notify user of successful svae MessageBox.Show($"{inputToSave} was successfully saved"); } catch (Exception ex) { // would show on write error MessageBox.Show(ex.Message); } }
/// <summary> /// We take the user input and append it to the end of the text file. We first make sure there is valid input. We concatenate the two user inputs from the text boxes /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnSave_Click(object sender, EventArgs e) { string userCollege = TxtBoxCollege.Text; string userCity = TxtBoxCity.Text; if (userCollege != "" && userCity != "") { string collegecity = $"{userCollege},{userCity}"; try { StreamWriter outputfile = File.AppendText(filename); outputfile.WriteLine(collegecity); outputfile.Close(); LstBox.Items.Add(collegecity); TxtBoxCity.Clear(); TxtBoxCollege.Clear(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }