我未能用 csv 文件名填充组合框。我通过从 Microsoft Visual Studio 的工具箱中拖动来创建组合框。我将组合框的名称设置为 ChooseSampleSheet。
以下是我的代码:
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";
}
另外,我尝试了以下代码:
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;
}
但它们都不起作用。我的组合框中没有显示任何内容。我希望看到 csv 文件名。这样我之后就可以单击打开选定的文件(不在我的代码中显示)。
人们建议我改变事件。以下是我的更新。
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)
{
}
但是,组合框中仍然没有显示任何内容。