0

I have this form which includes a combobox, and a listbox. The combobox has each subfolder inside the graphics folder as it's items. When the combobox's selected value changes the program will list every .png file inside the selected folder and add them to the listbox's items.

The problem is that without showing a messagebox in between adding the files to an array, and adding each item to the listbox, the array will stay empty.

Here's the code:

private void graphicBox_SelectedIndexChanged(object sender, EventArgs e)
{
    graphicList.Items.Clear();
    string selectedfolder = SkinSuite.Properties.Settings.Default.exepath + "\\GRAPHIC\\" + graphicBox.SelectedText;
    graphicfiles = Directory.GetFiles(SkinSuite.Properties.Settings.Default.exepath + "\\GRAPHIC\\" + graphicBox.SelectedText);
    // MessageBox.Show("FOR SOME REASON THIS DOESNT WORK IF I DONT SHOW YOU A MESSAGEBOX!");

    foreach (string file in graphicfiles)
    {
        graphicList.Items.Add(Path.GetFileName(file));
    }
}

If I were to uncomment the messagebox line, the code works just fine.

kAlekki
  • 3
  • 3
  • 2
    Do you mean that *graphicfiles* will be **null**, or do you rather mean that *graphicfiles* is an **empty array**? –  May 16 '14 at 16:46
  • What happens when you add the file string directly without getting the file name (As a test)? – Asad Ali May 16 '14 at 16:47
  • have you tried to add your 'for each' block in an Dispatcher.begininvoke to see what happening ? – Seb May 16 '14 at 16:47
  • @elgonzo Yes the graphicfiles array is empty. – kAlekki May 16 '14 at 16:53
  • @kAlekki, then please edit your question accordingly :) –  May 16 '14 at 16:55
  • By the way, to combine multiple strings to a path you should use `Path.Combine()` to avoid problems with backslashes – Flat Eric May 16 '14 at 16:55

1 Answers1

0

Use graphicBox.SelectedItem instead of graphicBox.SelectedText which is working fine for me. Please change your code like below, also use Path.Combine to build file path

private void graphicBox_SelectedIndexChanged(object sender, EventArgs e)
{
    graphicList.Items.Clear();
    string selectedfolder = SkinSuite.Properties.Settings.Default.exepath + "\\GRAPHIC\\" + graphicBox.SelectedItem;
    graphicfiles = Directory.GetFiles(SkinSuite.Properties.Settings.Default.exepath + "\\GRAPHIC\\" + graphicBox.SelectedText);
    // MessageBox.Show("FOR SOME REASON THIS DOESNT WORK IF I DONT SHOW YOU A MESSAGEBOX!");

    foreach (string file in graphicfiles)
    {
       graphicList.Items.Add(Path.GetFileName(file));
    }
 }
Vimal CK
  • 3,543
  • 1
  • 26
  • 47