Reputation: 432
I have a ListBox that is populated with entries from a folder consisting of text files (the list box is taking the names of the files only). I have trouble getting the list box to refresh every time I add files. You add files/errands by pressing a button in the main program, which brings out a second window in which you write your errand and choose its priority (low, medium or high).
The desired effect would be that the list box would update itself when adding new text files/errands, to include it, however, this is not the case at the moment, and I've tried following examples on the net by using DataStore
and Binding
among others, but none have worked so far. The main program looks like this:
P.S. The program is half-Swedish, but essentially, "Skapa lapp" = "Create errand", which is the only important one here.
And this image below is just to show you how the list box and the errands/text files work together (the text files are added to the list box by a foreach
loop).
When creating a new errand (Skapa lapp-button), you will be presented with a new window:
When writing your new errand in this window and choosing a priority level then pressing "Create errand" (or Skapa lapp), the following will happen on that button click (simplified version):
private string mappNamn = @"C:\Errands\";
Lapphantering uppdateraFönster = new Lapphantering();
private void buttonSkapaLapp_Click(object sender, EventArgs e)
{
try
{
//When choosing the low priority radio button, do this:
if (radioButtonLågPrio.Checked)
{
using (var file = new StreamWriter(Path.Combine(mappNamn, "1 - " + textBoxLappText.Text + ".txt")))
{
uppdateraFönster.listBoxLappar.Items.Add(textBoxLappText.Text);
uppdateraFönster.Update(); //This doesn't work.
uppdateraFönster.Refresh(); //Nor does this.
}
}
Back over to the main window (Lapphantering), the list box is only updated when you restart the application all over again and let the main program add the files by initializing the component:
public Lapphantering()
{
InitializeComponent();
//For each file, add new files to the list box.
DirectoryInfo dinfo = new DirectoryInfo(@"C:\Errands\");
FileInfo[] Filer = dinfo.GetFiles("*.txt");
mappNamn = dinfo.FullName;
foreach (FileInfo file in Filer)
{
listBoxLappar.Items.Add(Path.GetFileNameWithoutExtension(file.Name));
}
}
So, how can I refresh/update the list box every time I add a new errand/text file without having to restart the application each time?
Upvotes: 0
Views: 747
Reputation: 2889
Theproblem is that you are creating a new instance of Lapphantering and editting the value in there. Change this in your Program cs:
public static Lapphantering mainForm;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
mainForm = new Lapphantering(); // create instance of Lapphantering
Application.Run(mainForm);
}
then in your other window do this:
if (radioButtonLågPrio.Checked)
{
using (var file = new StreamWriter(Path.Combine(mappNamn, "1 - " + textBoxLappText.Text + ".txt")))
{
Program.mainform.listBoxLappar.Items.Add(textBoxLappText.Text);
Program.mainform.listBoxLappar.Update();
Program.mainform.listBoxLappar.Refresh(); // access mainform in Program
}
}
this should work your are working on one object in the program.cs
Upvotes: 4
Reputation: 83
You are looking for a file system monitor. This might help: https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx
Upvotes: 0