Search code examples
c#arrayswinformslistviewtextbox

How do I append file, clear form, load file and update listview on button click?


I have a directory form which displays the contents of a CSV file in a listview upon load. I am creating a button that sends three user-entered text fields to the CSV file, clears the form textboxes and listview, then loads and displays the updated directory in the listview.

Unfortunately, when the button is clicked, I instead end up with a blank listview, even though the clearForm method comes before loadDirectory and displayDirectory. When I comment out the clearForm function, my listview contains the original list followed by the entire new list, as expected.

private void BtnAddNew_Click(object sender, EventArgs e)
    {
        addRecord(); // Sends text box entries to a file via. streamreader *working*
        clearForm(); // Clears the form *working on standalone clear button*
        loadDirectory(); // Loads CSV file contents to array *working*
        displayDirectory(); // Displays array to listview *working*
    }


public void loadDirectory()
    {
        StreamReader sr = new StreamReader(path);
        int lineCount = File.ReadLines(path).Count();

        string line;
        int count = -1;
        directoryTable = new record[lineCount];

        while (!sr.EndOfStream)
        {
            count++;
            line = sr.ReadLine();
            string[] fields = line.Split(',');

            record currentRecord = new record();
            currentRecord.surname = fields[0];
            currentRecord.forename = fields[1];
            currentRecord.extCode = Convert.ToInt32(fields[2]);
            directoryTable[count] = currentRecord;
        }
        sr.Close();
    }


public void displayDirectory()
    {
        for (int counter = 0; counter < directoryTable.Length; counter++)
        {
            ListViewItem lvi = new ListViewItem();
            lvi.Text = (Convert.ToString(directoryTable[counter].surname));
            lvi.SubItems.Add(Convert.ToString(directoryTable[counter].forename));
            lvi.SubItems.Add(Convert.ToString(directoryTable[counter].extCode));
            lvDirectory.Items.Add(lvi);
        }
    }


public void addRecord()
    {
        string[] newRecord = new string[3];

        newRecord[0] = txtForename.Text;
        newRecord[1] = txtSurname.Text;
        newRecord[2] = txtExtCode.Text;

        // Write newRecord array to last line of directory file
        StreamWriter sw = new StreamWriter(path, append: true);
        sw.WriteLine(newRecord[0] + ", " + newRecord[1] + ", " + newRecord[2]);
        sw.Close();
    }


public void clearForm()
    {
        foreach (Control field in Controls)
        {
            if (field is TextBox)
                ((TextBox)field).Clear();
            else if (field is ListView)
                ((ListView)field).Clear();
        }
    }

Solution

  • You need to clear the items in the listview and not the listview itself:

    public void clearForm()
    {
        foreach (Control field in Controls)
        {
            if (field is TextBox)
                ((TextBox)field).Clear();
            else if (field is ListView)
                ((ListView)field).Items.Clear();
        }
    }