Home > OS >  C# Add file names to labels
C# Add file names to labels

Time:07-16

I need to display the first 20 files from a folder into 20 labels within my Windows Form. My code fills all the 20 labels with only one file name. Someone help me to fix my code to fill each file to it's label.

This is what I've tried to work with;

private void btnGetFiles_Click(object sender, EventsArgs e)
{
string filePath=Application.StartupPath  @"\PDF Scans";

if(Directory. Exists(filePath))
{
    DirectoryInfo pdfPath=new DirectoryInfo(filePath):
    
    FileInfo[] Files=pdfPath. GetFiles():
    
    foreach(FileInfo fi in Files)
    {
        lblFile1.Text=fi.Name:
        lblFile2.Text=fi.Name:
        lblFile3.Text=fi.Name:
        lblFile4.Text=fi.Name:
        lblFile5.Text=fi.Name:
        lblFile6.Text=fi.Name:
        lblFile7.Text=fi.Name:
        lblFile8.Text=fi.Name:
        lblFile9.Text=fi.Name:
        lblFile10.Text=fi.Name:
        lblFile11.Text=fi.Name:
        lblFile12.Text=fi.Name:
        lblFile13.Text=fi.Name:
        lblFile14.Text=fi.Name:
        lblFile15.Text=fi.Name:
        lblFile16.Text=fi.Name:
        lblFile17.Text=fi.Name:
        lblFile18.Text=fi.Name:
        lblFile19.Text=fi.Name;
        lblFile20.Text=fi.Name;          
    }
}
else
{
    MessageBox.Show(filePath   "doesn't exist"):
}
}

CodePudding user response:

Let's keep this simple with the smallest change in your code. The problem in your code is that you assign the same name to all labels. You can assign each name to its corresponding label, if you change your code a bit.

Windows Forms has a Controls property containing all controls and you can access them by name, which we can build from index when using for, so:

for (int i = 0; i < Files.Length; i  )
{
    Controls["lblFile"   (i   1).ToString()].Text = Files[i].Name;
}

will do that.

You must make sure that the number of files are less than or equal to the number of labels.

CodePudding user response:

You have syntax errors in your code, use ; after angle bracket not :, second thing in your loop you put same value in label Text property for each iteration.

As someone suggested in comments put all labels in some collection, for example list.

private void btnGetFiles_Click(object sender, EventsArgs e)
{
    string filePath=Application.StartupPath  @"\PDF Scans";

    if (Directory.Exists(filePath))
    {
        DirectoryInfo pdfPath = new DirectoryInfo(filePath);

        FileInfo[] files = pdfPath.GetFiles().Take(20).ToArray();

        List<Label> labels = new List<Label>();

        foreach (var control in this.Controls)
        {
            if (control is Label)
            {
                labels.Add((Label)control);
            }
        }

        var labelFileList = files.Zip(labels, (File, Label) => new { File, Label });

        foreach (var labelFile in labelFileList)
        {
            labelFile.Label.Text = labelFile.File.Name;
        }
    }
    else
    {
        MessageBox.Show(filePath   "doesn't exist");
    }
}
  • Related