0

I am trying to develop an application using Windows Form to read the contents of a *.txt file with contains information below:

12.14,12.00,11.98,12.01,12.09,12.05,
11.50,11.48,11.56,11.52,11.53,11.48,
12.08,11.13,11.12,12.08,11.12,12.10,
12.14,12.00,11.98,12.01,12.09,12.05,
11.50,11.48,11.56,11.52,11.53,11.48,
12.09,11.12,11,09,12.12,11.16,12.12,
12.14,12.00,11.98,12.01,12.09,12.05,
11.50,11.48,11.56,11.52,11.53,11.48,
12.17,11.11,11.19,12.15,11.10,12.11,
12.14,12.00,11.98,12.01,12.09,12.05,

and need to copy each of individual values into a TextBox (there are a total of 60 TextBox's).

I tried to use code below, but I have no idea how to work with all TextBox's individually.

using System;
using System.IO;

public class Sample
{
    public static void Main() {
        using (StreamReader reader = new StreamReader("yourfile.txt")) {
            string line = null;
            while (null != (line = reader.ReadLine())) {
                string[] values = line.Split(',');
                float[] numbers = new float[values.Length - 1];
                for (int i = 1; i < values.Length - 1; i++)
                    numbers[i - 1] = float.Parse(values[i]);

                
            }
        }
    }
}

(this code was copied from URL How to read values from a comma separated file?)

Peter Duniho
  • 66,412
  • 6
  • 96
  • 128
Claude
  • 29
  • 4
  • 1
    you have done all the hard work all that remains is yourTxtBox.Text = numbers[i].ToString(); – apomene Sep 18 '20 at 14:59
  • 1
    Make an array of text boxes to simplify code : TextBox[] textboxes = { textBox1, textBox2, textBox3 }; – jdweng Sep 18 '20 at 15:15
  • 1
    Without more context, it's impossible to know for certain what answer you are looking for. However, see duplicate for the basic strategy for dealing with multiple text boxes in a window. You will need to do something similar, so that you can address each text box in turn within your loop. – Peter Duniho Sep 18 '20 at 16:49

1 Answers1

0

A simple solution i can suggest to you is that i assume your text order should match a specific order in the textbox as well. Take all your 60 textbox and set their Tag property to the integer value of their index zero based. One will be 0 and the last 59.

Then in your code, let's say it currently run in the Form itself you just change the code to this :

private void LoadFile() 
{
    // get all textbox in the form with some sort of tag
    var textboxes = this.Controls.OfType<TextBox>().ToList().Where(txt => txt.Tag != null).ToList();

    // will contain all numbers from the file ordered
    var orderedNumbers = new List<float>();

    using (StreamReader reader = new StreamReader("yourfile.txt")) {
        string line = null;
        while (null != (line = reader.ReadLine())) {
            string[] values = line.Split(',');
            float[] numbers = new float[values.Length - 1];
            for (int i = 1; i < values.Length - 1; i++)

                numbers[i - 1] = float.Parse(values[i]);
            // add the numbers of the line to the collection
            orderedNumbers.AddRange(numbers);
            
        }
    }

    // loop to update the textboxes
    for(int i = 0; i < orderedNumbers.Count; i++)
    {
        // find the textbox with current index
        var textBox = textboxes.FirstOrDefault(txt => txt.Tag.ToString() == i.ToString());

        // if we found the text box we can update
        if(textBox != null)
        {
            textBox.Text = orderedNumbers[i].ToString();
        }
    }
}
Franck
  • 4,310
  • 1
  • 27
  • 50
  • Could not convert Float to String, is there a way to do that before show the results? textBox.Text = orderedNumbers[i]; – Claude Sep 18 '20 at 19:05
  • I just updated but all you need is to call the `.ToString()` on the value of the collection – Franck Sep 18 '20 at 19:36
  • I really appreciate your help, but it did not work for me. The first time I ran the code it gave me the error in `public static void Main()` where I cannot use the keyword `this` in a static field. So after removed the 'static' to ''public void Main()' did work neither. Thank you. – Claude Sep 18 '20 at 21:43
  • Sorry i copied your code and didn't check if it was good. That method needs to be in your form – Franck Sep 21 '20 at 11:49