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);
    }
}
}

`

I jumped the gun too quick I guess, I think I have it figured out, I had to move a button event up and it seemed to take off one of the duplicates. The other duplicate I know I'll have to have an if statement or something like that to stop it from doing that. Thanks for looking.

Okay, so I guess I'm still having issues displaying in the text box when I click enter. It shows nothing and I've tried to use
lstRecords.Text = string.Format(lastName.Text + "\t" + firstName.Text + (":") + "\t" + idBox.Text + "\t" + classBox.Text + "\t" + gradeBox.Text);
and I've also tried using the same code that i use for the save box to display it lstRecords.Items.Add(lastName.Text + "\t"+ firstName.Text+(":") + "\t" + idBox.Text + "\t" + classBox.Text + "\t" + gradeBox.Text); but I think that may have been my original problem. I'm trying to get the program to enter entries into the box with enter, then save using save as without doubling up. Any hints, ideas or comments would be helpful.

Okay, I'm seeing my main issue I think now, it's set up for csv, not individual textboxes, I wasn't taught how to do that. I'm going to go do some more reading.

I think I've found my information. When I complete my file I'll be happy to share what I have. Have a good day.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.