Okay I'm here with a question about a form I'm creating. It's a simple form that reads in the input from text boxes. I should be able to enter the text, and press my enter button to display it in the display box. I should also be able to save what it is displaying. When I click enter, it displays the information correctly. When I click save as, it adds two duplicates(a total of 3 lines of the information) to the text file. Where am I going wrong? I thought that the butten events would control this for me. I'm also needing to disable the enter box when there's no text in the fields, but that's not what I'm worried about right now, I'm more concerned about the part that's already coded and not working the way I thought it would.
`using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace GradeEntry
{
public partial class Form1 : Form
{
//public vars for file access
FileStream output;
StreamReader fileReader;
StreamWriter fileWriter;
bool bCheck;
public Form1()
{
InitializeComponent();
}
private void btnOpen_Click(object sender, EventArgs e)
{
OpenFileDialog fileChooser = new OpenFileDialog();
fileChooser.Title = "Pick a file";
fileChooser.Filter = "Text Files (*.txt)|*.txt";
DialogResult result = fileChooser.ShowDialog();
// did they click cancel?
if (result == DialogResult.Cancel)
{
//do nothing
return;
}
//proceed with processing file
// get the file name they chose
string strFileName = fileChooser.FileName;
try
{
// open the file for read access
output = new FileStream(strFileName,
FileMode.Open, FileAccess.Read);
fileReader = new StreamReader(output);
//var to hold the read record
string strInputLine;
string[] fields;
//loop to get records and break into fields
while (fileReader.EndOfStream != true)
{
//read a record
strInputLine = fileReader.ReadLine();
//split the record
fields = strInputLine.Split(',');
//add to the listbox
lstRecords.Items.Add(lastName.Text + "\t"+ firstName.Text+(":") + "\t" + idBox.Text + "\t" + classBox.Text + "\t" + gradeBox.Text);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//always close resources
fileReader.Close();
output.Close();
}
}
private void btnCreate_Click(object sender, EventArgs e)
{
//create a save file dialog
SaveFileDialog fileChooser = new SaveFileDialog();
fileChooser.Title = "Choose Save Location";
fileChooser.Filter = "Text Files (*.txt)|*.txt";
//open dialog, get result
DialogResult result = fileChooser.ShowDialog();
//did they click cancel?
if (result == DialogResult.Cancel)
{
return;
}
//get the file name from the dialog
string strFileName = fileChooser.FileName;
try
{
//save via filestream
//open the new file for write access
if (bCheck)
{
output = new FileStream(strFileName,
FileMode.OpenOrCreate, FileAccess.Write);
bCheck = false;
}
else
{
output = new FileStream(strFileName,
FileMode.Append, FileAccess.Write);
}
fileWriter = new StreamWriter(output);
//take the text from the box and write it to the file
fileWriter.WriteLine(lastName.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//close resources
fileWriter.Close();
output.Close();
}
}
private void enterButton_Click(object sender, EventArgs e)
{
lstRecords.Items.Add(lastName.Text + "\t" + firstName.Text + (":") + "\t" + idBox.Text + "\t" + classBox.Text + "\t" + gradeBox.Text);
}
}
}
`