Hi!

I am trying to create an application in C# that will take a list of IPs from a text file and display whether or not a ping is returned from each IP.
I was wondering if it was possible, with a loop or something similar, to create two labels for each IP, one for the name and one for the status, dynamically, every time the program is run.

How would I go about doing this?

Thanks!

Read through the text file line by line in a loop.
And for each iteration, declare the two labels and add them to the forms Control collection.

int counter = 0;
string line;
int lastKnownTop = 10; //Or whereever you want the first set of labels to be placed

System.IO.StreamReader file = new System.IO.StreamReader("path");
while ((line = file.ReadLine()) != null)
{
    Label ip = new Label();
    ip.Position(new Point(10, lastKnownTop));
    ip.Text = line;

    Label status = new Label();
    status.Position(new Point(25, lastKnownTop));
    status.Text = CheckStatus(ip).ToString();

    form1.Controls.Add(ip);
    form1.Controls.Add(status);

    lastKnownTop = (ip.Top + ip.Height) + 5;
}
file.Close();

You can do in a loop:

int x = 20, y = 20; //initial location x and y
for(int i = 0; i < 2; i++)
{
    Label l = new Label();
    l.Position = new Point(x, y);
    l.Text = i == 0 ?" Text for label 1" : "Text for label 2";
    this.Controls.Add(l);
    //set new location for next:
    x+= 50; //2nd will be 50pixels to the right
}

Oh! I see.

So the form has a collection of controls you can just add to? That's good to know!

Thanks! I will attempt it later!

:)

This article has been dead for over six months. Start a new discussion instead.