0

When I click the button the first time, it works, but after that, it doesn't do anything. I have tried to debug this and I have looked it up but I can't find an answer. I am probably just not noticing something obvious.

    private void button5_Click(object sender, EventArgs e)
    {
        string[] files;

        files = Directory.GetFiles("Tasks");

        foreach (string file in files)
        {
            string[] lines;
            StreamReader reader = new StreamReader(file);
            lines = File.ReadAllLines(file);
            tasks.Add(lines[0]);
            reader.Close();
        }

        listBox1.DataSource = tasks;
    }
dferdo61
  • 60
  • 2
  • 11

1 Answers1

1

You're problem is not with the button. It's with the listbox. It's not updating properly because it's not detecting a change in your datasource.

Try setting it to null before updating so it knows it's changing:

listBox1.DataSource = null;
listBox1.DataSource = tasks;

Alternative Method:

You can also use a BindingList instead of a regular list.

See here: How to refresh DataSource of a ListBox in C# WinForms

Community
  • 1
  • 1
Clay07g
  • 1,105
  • 7
  • 23