Click here to Skip to main content
14,268,557 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello guys!
This is my first question here in codeproject so please be nice with me.


I have written a method that create lables according to the number of rows inside a text file.

public void floorName()
        {
            int numOfLines = File.ReadAllLines(@"C:\NoosApp\Details\Details.txt").Length;
            for (int i = 0; i < numOfLines; i++)
            {
                System.Windows.Forms.Label lbl = new System.Windows.Forms.Label();
                this.Controls.Add(lbl);
                lbl.Top = B * 58;
                lbl.Left = 1400;
                lbl.Width = 350;
                lbl.Height = 50;
                lbl.AutoSize = false;
                lbl.TextAlign = ContentAlignment.MiddleRight;
                lbl.Text = "" + this.B.ToString();
                lbl.ForeColor = Color.Black;
                lbl.Font = new Font("Fb reforma", 36);
                lbl.BackColor = Color.White;
                B = B + 1;
            }


And it's work just fine.
And i also succeeded to get the data from the text file line by line into array like this >>
string [] lines = File.ReadAllLines(@"C:\NoosApp\Details\floor.txt");


What I have tried:

But i have tried to store the data in each label that the method created in the code above without any success.

Please help me, i am beginner in C#
Thanks
Posted
Updated 13-Aug-19 1:02am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

This should give you some ideas:
// required
using System;
using System.IO;
using System.Windows.Forms;

// in some class
public void CreateLabelsFromFile(string filepath)
{
    if(! File.Exists(filePath))
    {
       throw new FileNotFoundError("invalid filepath");
    }
    
    try
    {
        Lines = File.ReadAllLines(filepath);
    }
    catch (Exception ex)
    {
        throw new IOException("can't read file: " + ex.Message);
    }

    int nlines = Lines.Length;

    if(nlines == 0)
    {
        throw new FileLoadException("empty file");
    }

    int vspace = 0;
    
    for (int i = 0; i < nlines ; i++)
    {
        // create the Label and configure it

        lbl.Text = Lines[i];
        
        vspace++;
    }
}
Reading files is often error prone; you should always check if the File exists and/or if the Directory exists; always open/read the file in a Try/Catch block: a variety of errors can occur.

The "convenience" file read methods, like 'ReadAllLines, do not require bracketing the method in a 'using statement, as other File methods do.

See: [^]
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

If you would like to get data from text file and display it in labels, you have to read them from array of lines.

I've changed your code a bit...

string [] lines = File.ReadAllLines(@"C:\NoosApp\Details\floor.txt");
int numOfLines = lines.Length;
for (int i = 0; i < numOfLines; i++)
{
    System.Windows.Forms.Label lbl = new System.Windows.Forms.Label();
    lbl.Top = i * 58;
    lbl.Left = 1400;
    lbl.Width = 350;
    lbl.Height = 50;
    lbl.AutoSize = false;
    lbl.TextAlign = ContentAlignment.MiddleRight;
    lbl.Name = "Label"  + i.ToString(); //add name to label
    lbl.Text = lines[i]; //display text from text file, specific line
    lbl.ForeColor = Color.Black;
    lbl.Font = new Font("Fb reforma", 36);
    lbl.BackColor = Color.White;
    this.Controls.Add(lbl);
}


Note #1: You don't need to read text file twice to get the numer of lines and lines itself!
Note #2: You don't need [B] variable, because you can use [i] inside a for loop.

Please, read carefully solution #1 by BillWoodruff[^], because it contains few very important suggestions.
   
v2
Comments
BillWoodruff 14-Aug-19 2:18am
   
"You don't need [B] variable" ... and yet, you use it :)
Maciej Los 14-Aug-19 2:55am
   
Hawk eye. Thanks for pointing that out, Bill.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100