I am trying to get multiple values from a text file delimited by commas and copy them into a total of 12 textboxes (eg: value '1' goes to textBox1, value '2' goes to textBox2 and so on...)
I am using Windows Forms and I am getting the error below:
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
The values from my text files are these:
1,2,3,4,5,6,
7,8,9,10,11,12,
Below is what I tried so far:
namespace WindowsFormsApp18
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class Cliente
{
public string DUTA1A { get; set; }
public string DUTA1B { get; set; }
public string DUTA1C { get; set; }
public string DUTA1D { get; set; }
public string DUTA1E { get; set; }
public string DUTA1F { get; set; }
public string DUTA2A { get; set; }
public string DUTA2B { get; set; }
public string DUTA2C { get; set; }
public string DUTA2D { get; set; }
public string DUTA2E { get; set; }
public string DUTA2F { get; set; }
}
private void button1_Click(object sender, EventArgs e)
{
System.IO.StreamReader reader = new System.IO.StreamReader(@"C:\\DUTA\YourFile.txt");
string line;
while ((line = reader.ReadLine()) != null)
{
string[] dados = line.Split(',');
int codigo = int.Parse(dados[0]);
string DUTA1A = dados[0];
string DUTA1B = dados[1];
string DUTA1C = dados[2];
string DUTA1D = dados[3];
string DUTA1E = dados[4];
string DUTA1F = dados[5];
string DUTA2A = dados[6];
string DUTA2B = dados[7];
string DUTA2C = dados[8];
string DUTA2D = dados[9];
string DUTA2E = dados[10];
string DUTA2F = dados[11];
textBox1.Text = DUTA1A;
textBox2.Text = DUTA1B;
textBox3.Text = DUTA1C;
textBox4.Text = DUTA1D;
textBox5.Text = DUTA1E;
textBox6.Text = DUTA1F;
textBox7.Text = DUTA2A;
textBox8.Text = DUTA2B;
textBox9.Text = DUTA2C;
textBox10.Text = DUTA2D;
textBox11.Text = DUTA2E;
textBox12.Text = DUTA2F;
}
}
}
}
Any suggestions?
What I have tried:
Using 6 values there is no errors. Tried to modify the indexes too.