-1

I failed to fill combobox with csv filenames. I created the combobox by dragging from toolbox in Microsoft Visual Studio. I set the name of combobox to ChooseSampleSheet.

The following is my code:

private void ChooseSampleSheet_SelectedIndexChanged(object sender, EventArgs e)
{
  DirectoryInfo d = new DirectoryInfo(@"C:\Users\UniFlow\Desktop\Europa-master\user interface\Europa design Y\Experiemnt_Gui");//Assuming Test is your Folder
  FileInfo[] Files = d.GetFiles("*.csv"); //Getting Text files
  ChooseSampleSheet.DataSource = Files;
  ChooseSampleSheet.DisplayMember = "Name";
}

Also, I tried the following code:

private string path = (@"C:\Users\UniFlow\Desktop\Europa-master\user interface\Europa design Y\Experiemnt_Gui");

private void ChooseSampleSheet_SelectedIndexChanged(object sender, EventArgs e)
{
  List<String> Configurations = Directory.EnumerateDirectories(path, "*.exe")
                                         .Select(p => Path.GetFileName(p))
                                         .ToList();
  ChooseSampleSheet.DataSource = Configurations;
}

But Neither of them works. Nothing shows in my combobox. I expected to see csv file names. So that I can click to open selected file afterwards (not show in in my code).

People suggested me to change the event. The following is my update.

private void form4_load(object sender, EventArgs e)
    {
        DirectoryInfo d = new DirectoryInfo(@"C:\Users\UniFlow\Desktop\Europa-master\user interface\Europa design Y\Experiemnt_Gui");//Assuming Test is your Folder
        FileInfo[] Files = d.GetFiles("*.csv"); //Getting Text files

        ChooseSampleSheet.DataSource = Files;
        ChooseSampleSheet.DisplayMember = "Name";
    }



    private void ChooseSampleSheet_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

However, nothing show in the combobox still.

Ying Du
  • 11
  • 4
  • Did you use the debugger? Breakpoints to determine if the **d** and **Configurations** holds the values you need to populate the combobox? – HEWhoDoesn'tKnow Dec 10 '18 at 23:09
  • Why would you run that code in the SelectedIndexChanged event? That event only runs when something is selected in the list, which probably is still empty. – LarsTech Dec 10 '18 at 23:35
  • Did you "subscribe" to the load event, or just copied the load event? Use the OnLoad override instead, put your code in there. – LarsTech Dec 10 '18 at 23:41

1 Answers1

0

I do not see anything wrong in your code however I think your code is at the wrong place. SelectedIndexChanged will get execute when you select something from the drop down. Since your drop down has not been filled with values , you can't fire that event.

Put the same code in form_load and you will see the values there.

     DirectoryInfo d = new DirectoryInfo(@"C:\Users\UniFlow\Desktop\Europa-master\user interface\Europa design Y\Experiemnt_Gui");//Assuming Test is your Folder
    FileInfo[] Files = d.GetFiles("*.csv"); //Getting Text files

    ChooseSampleSheet.DataSource = Files;
    ChooseSampleSheet.DisplayMember = "Name";
Gaurav
  • 623
  • 5
  • 11
  • Hi, I move the code to form4_load, but nothing show in combobox still. I am going to edit my post. So that I can show my code. – Ying Du Dec 10 '18 at 23:36
  • Do you have CSV file in that folder? I tried in my local machine and it worked fine. – Gaurav Dec 10 '18 at 23:43
  • Also open the form in design mode, double click on the form. It will write form_load event for you. Put your code there and let me know. – Gaurav Dec 10 '18 at 23:56
  • Yes! "open the form in design mode, double click on the form. It will write form_load event for you." solved my problem! Thank you so much!! – Ying Du Dec 11 '18 at 00:13