dferdo_04 dferdo_04 -4 years ago 86
C# Question

C# Button not firing more than once

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

listBox1.DataSource = tasks;

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download