tobulos1 tobulos1 - 1 year ago 246
C# Question

C# - Update ListBox from another class not working

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

among others, but none have worked so far. The main program looks like this:

How the errand system looks like.

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

Here is the actual files that are added in the list box.

When creating a new errand (Skapa lapp-button), you will be presented with a new window:

The window when creating a new errand.

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)
//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.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()

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

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?

Answer Source

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>

    static void Main()
        mainForm = new Lapphantering(); // create instance of Lapphantering

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.Refresh(); // access mainform in Program 

this should work your are working on one object in the program.cs

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