Create a label when reading data from a file

Asked

Viewed 54 times

0

I am trying to create a button that, using the information deposited in 3 files (labelName, Labelx, Labely), create a number of cells according to the number of entries.

That is, if I have 1 entry in each file (a name in Labelname, a number in Labelx, another number in Labely) will create a label with the name mentioned in the Top and Left positions pointed in the Labelx and Labely files respectively.

At this point the problem is that I don’t know how to make my cycle pass the first input and so what I have is a repetition of the first values creating multiple times the first based label in this data (in the photo marked in blue).

My code is this::

 private void button4_Click(object sender, EventArgs e)
 {
       for (int i = 0; i < labelList.Count(); i++)
       {
                Label l = new Label();
               //NOTA: tentativa anterior = *exemplo_abaixo*
                
                TextReader oner = new StreamReader("labelName.txt");
                string ltext = oner.ReadLine();
                
                TextReader twor = new StreamReader("labelX.txt");
                string labelLeft = twor.ReadLine();

                TextReader threer = new StreamReader("labelY.txt");
                string labelTop = threer.ReadLine();


                l.Text = ltext;
                l.Left = Convert.ToInt32(labelLeft);
                l.Top = Convert.ToInt32(labelTop);

                Controls.Add(l);
                ControlExtension.Draggable(l, true);

                oner.Close();
                twor.Close();
                threer.Close();

        }
  }

I just wanted to understand how I get my cycle past the first value of each file and create the rest of the Abels.

Thank you.

  • You have to save the values out of the for after every run of the increments the value that is saved, for example guards int Valortop=0; then at each cycle Valortop+=Valuewhat you want to save next;

  • I don’t understand... you always read the same files and expect to receive different values?

  • @Leandroangelo no, I want the different values in the file but I only get the first value of each file instead of the ones I have on each line

  • Wouldn’t it be because you are always reading the first line? When seeing your code it seems to me that there are some flaws in the logic and structure, which is labelList.Count()? What happens if there are more Abels in the list at what coordinates in the files? Why three files, one with each type of information?

  • @Leandroangelo labelList.Count() was used to count the number of Abels that went to the file, it is only used with this effect as a way to avoid having more or less Abels. Each of these Abels will have Top and Left coordinates and each has been placed on their list for the same reason - no more or less values (or at least to try). This way the first data of each list corresponds to the data in the same position in another list. Could it be less complicated? Possibly. But I’m not very experienced in c# and I learn as I do...

  • What matters are the lines that are registered in the files?

  • yes. The labelList.Count() is only one variable for the cycle to have an end

Show 2 more comments

1 answer

1


My suggestion is to store all label information in a single file, separating the attributes by ;. If what matters is the information that has been persisted, not the need to per other loop for this

labelList.txt

label1;774;592
192.168.1.156;600;222
PCZ;607;284

And in the code just read this file mounting your controls

private void button4_Click(object sender, EventArgs e)
{
    using (TextReader leitor = new StreamReader("labelList.txt"))
    {
        string linha;
        while ((linha = leitor.ReadLine()) != null)
        {
            var linhaDados = linha.Split(';');

            Label label = new Label
            {
                Text = linhaDados[0],
                Left = Convert.ToInt32(linhaDados[1]),
                Top = Convert.ToInt32(linhaDados[2])
            };

            Controls.Add(label);
            ControlExtension.Draggable(label, true);
        }
    }
}
  • @Mako, see if you can understand the code above and if it meets your need.

  • From what I can understand I have already used the methods it presents above during my learning, however I did not know the method Split that shows above still. I just had to adapt the code to insert into the file to just insert into a file and it works fine. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.