/// <summary> /// Saves the user input to the .txt file /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnSave_Click(object sender, EventArgs e) { StreamWriter outputFile; try { // obtain user input and format for save string statecap = $"{TxtBoxState.Text}, {TxtBoxCapital.Text}"; // open outputFile = File.AppendText(filepath); // use outputFile.WriteLine(statecap); // close outputFile.Close(); // clear results and reset cursor focus to first box TxtBoxCapital.Clear(); TxtBoxState.Clear(); TxtBoxState.Focus(); // update lst box LstBoxStateCaps.Items.Add(statecap); // notify user of successful save MessageBox.Show($"{statecap} 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. We append the text and close the file. We also add the concatenated string to the list so it is displayed on the form. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnSave_Click(object sender, EventArgs e) { string userState = TxtBoxState.Text; string userCapital = TxtBoxCapital.Text; if (userState != "" && userCapital != "") { string statecap = userState + "," + userCapital; try { StreamWriter outputFile = File.AppendText(filename); outputFile.WriteLine(statecap); outputFile.Close(); LstBoxStateCaps.Items.Add(statecap); TxtBoxState.Clear(); TxtBoxCapital.Clear(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }